gpt4 book ai didi

python - 在 Python 中从 Paypal 获取访问 token - 使用 urllib2 或请求库

转载 作者:太空宇宙 更新时间:2023-11-03 16:26:56 26 4
gpt4 key购买 nike

curl

curl -v https://api.sandbox.paypal.com/v1/oauth2/token \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-u "client_id:client_secret" \
-d "grant_type=client_credentials"

参数:-uclient_id:client_secret

这里我传递了我的 client_idclient_secret,它在 cURL 中正常工作。

我正在尝试在 Python 上实现相同的东西

python

import urllib2
import base64
token_url = 'https://api.sandbox.paypal.com/v1/oauth2/token'
client_id = '.....'
client_secret = '....'

credentials = "%s:%s" % (client_id, client_secret)
encode_credential = base64.b64encode(credentials.encode('utf-8')).decode('utf-8').replace("\n", "")

header_params = {
"Authorization": ("Basic %s" % encode_credential),
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json"
}
param = {
'grant_type': 'client_credentials',
}

request = urllib2.Request(token_url, param, header_params)
response = urllib2.urlopen(request)
print "Response______", response

回溯:

result = urllib2.urlopen(request)

 HTTPError: HTTP Error 400: Bad Request

你能告诉我我的 python 代码有什么问题吗?

最佳答案

迟到的答案,但截至 2021 年,我使用以下 python 代码生成新的不记名 token :

如果您还没有这样做,create a new live app on developer.paypal.com
您将收到一个 Client ID 和一个 Secret,您将使用它们来生成 Bearer Token。

enter image description here

Python 代码:

import requests

d = {"grant_type" : "client_credentials"}
h = {"Accept": "application/json", "Accept-Language": "en_US"}

cid = "ASOGsGWr7yxepDuthbkKL-WoGNVAS7O0XlZ2ejcWsBA8ZXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
secret = "EJTKAFEYfN9IaVHc4Y-MECzgBivt2MfW6rcyfbVky0T07yRwuuTdXOczuCoEIXXXXXXXXXXXXXXX"

r = requests.post('https://api.paypal.com/v1/oauth2/token', auth=(cid, secret), headers=h, data=d).json()
access_token = r['access_token']

来源:

  1. > https://developer.paypal.com/developer/applications/
  2. > https://www.paypal.com/smarthelp/article/how-do-i-get-an-access-token-ts2128

关于python - 在 Python 中从 Paypal 获取访问 token - 使用 urllib2 或请求库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31476540/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com