Отправка post-запроса в PayPal для получения токена доступа в Django

#python #django #api #paypal

#python #django #API #paypal

Вопрос:

Я пытаюсь отправить post-запрос в PayPal, чтобы получить токен доступа, чтобы начать выполнять еще несколько запросов.

это выдает 401 Unauthorized ошибку, вот мой код

 def buy_confirm(request, amount, price, server_name, char_name):
    if request.method == 'POST':
        client_id = 'xxxxxxxxxxxxxxxxxxxxx'
        client_secret = 'xxxxxxxxxxxxxxxxx'
        headers = {
            'Content-Type':'application/x-www-form-urlencoded',
            'Authorization':"Basic "   client_id   ' '   client_secret
        }
        body = {
            'grant_type':'client_credentials'
        }
        r = requests.post("https://api.sandbox.paypal.com/v1/oauth2/token",body, headers)
        print(r.status_code, r.reason)
    return render(request, 'buy_confirm.html')
  

Ответ №1:

Обнаружил, что я должен изменить его, чтобы он был таким :

 def buy_confirm(request, amount, price, server_name, char_name):
    if request.method == 'POST':
        client_id = 'xxxxxxxxxxxxxx'
        client_secret = 'xxxxxxxxxxxxxxx'
        headers = {
            'Content-Type':'application/x-www-form-urlencoded',
        }
        body = {
            'grant_type':'client_credentials'
        }
        r = requests.post("https://api.sandbox.paypal.com/v1/oauth2/token",body, headers, auth=(client_id, client_secret))
        print(r.status_code, r.reason)
    return render(request, 'buy_confirm.html')