简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM