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"参数:-u采取client_idclient_secret
这里我传递我的client_id和client_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回溯:
结果=urllib2.urlopen(请求) HTTPError: HTTP错误400:错误请求
你能告诉我我的python代码有什么问题吗?
发布于 2020-03-25 22:01:47
迟答,但从2021年起,我使用以下python代码生成一个新的承载令牌:
如果你还没这么做,在developer.paypal.com上创建一个新的实时应用程序
您将收到一个Client ID和一个Secret,您将使用它们来生成Bearer。

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']资料来源:
发布于 2015-07-17 13:24:58
我建议使用请求:
import requests
import base64
client_id = ""
client_secret = ""
credentials = "%s:%s" % (client_id, client_secret)
encode_credential = base64.b64encode(credentials.encode('utf-8')).decode('utf-8').replace("\n", "")
headers = {
"Authorization": ("Basic %s" % encode_credential),
'Accept': 'application/json',
'Accept-Language': 'en_US',
}
param = {
'grant_type': 'client_credentials',
}
url = 'https://api.sandbox.paypal.com/v1/oauth2/token'
r = requests.post(url, headers=headers, data=param)
print(r.text)发布于 2015-07-17 13:28:01
它需要URL编码:
param = {
'grant_type': 'client_credentials',
}
data = urllib.urlencode(param)
request = urllib2.Request(token_url, data, header_params)https://stackoverflow.com/questions/31476540
复制相似问题