将发布请求发送到PayPal以获取Django中的访问令牌

时间:2019-04-21 14:24:34

标签: python django api paypal

我正在尝试向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 个答案:

答案 0 :(得分:0)

发现我必须将其更改为这样:

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