Ahmed Wagdi
Ahmed Wagdi

Reputation: 4391

Sending post request to PayPal to get access token in Django

Am trying to send a post request to PayPal to get the access token to start doing some more requests.

it gives me 401 Unauthorized Error, here is my code

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')

Upvotes: 2

Views: 510

Answers (1)

Ahmed Wagdi
Ahmed Wagdi

Reputation: 4391

Found out that I have to change it to be like this :

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')

Upvotes: 2

Related Questions