这周的大部分时间我都在研究ebaySDK。我已经设法将交易和购物API集成到我的项目中。对于交易API,我使用了一个Auth n Auth令牌,有效期长达18个月。我需要用于Sell API的OAuth令牌只有一天有效,所以我需要在它过期之前定期获取它。我遵循了网站上的文档,甚至尝试在github上查看python代码库,但到目前为止我还无法继续前进。这是我的请求代码的一个快速片段,我做错了什么?
import requests, json, base64, xmltodict
AppSettings = {
'app_id' : 'my_app_id',
'app_secret' : 'my_app_secret',
'dev_id': 'my_dev_id',
'ruName': 'the_ruName_for_my_app'
}
authHeaderData = AppSettings['app_id']+':'+AppSettings['app_secret']
encodedAuthHeader = base64.b64encode(authHeaderData)
session = requests.Session()
print encodedAuthHeader
url = 'https://api.ebay.com/identity/v1/oauth2/token'
session.headers.update({
'Content-Type':'application/x-www-form-urlencoded',
'Authorization':'Basic '+encodedAuthHeader
})
data = {
'grant_type':'client_credentials',
'redirect_uri': AppSettings['ruName'],
'scope':'https://api.ebay.com/oauth/api_scope'
}
response = session.post(url, data=data).json()
print response
我得到的回应是:
{u'error_description': u'client authentication failed', u'error': u'invalid_client'}
我检查了所有的钥匙。我甚至试图通过ebay提供的产品登录来获得令牌,但无济于事。我从ebay提供的url得到的回应是html和js代码(没有JSON或任何数据)。
有没有人遇到过类似的问题?我该如何解决这个问题呢?我的请求是不是错了?任何见解都是非常感谢的。
发布于 2018-06-27 05:12:09
因为遍历eBays文档来找到这个答案是一场噩梦,我想我应该发布我的函数来解决这个问题。
import requests, urllib, base64
def getAuthToken():
AppSettings = {
'client_id':'<client_id>',
'client_secret':'<client_secret>',
'ruName':'<ruName>'}
authHeaderData = AppSettings['client_id'] + ':' + AppSettings['client_secret']
encodedAuthHeader = base64.b64encode(str.encode(authHeaderData))
headers = {
"Content-Type" : "application/x-www-form-urlencoded",
"Authorization" : "Basic " + str(encodedAuthHeader)
}
body= {
"grant_type" : "client_credentials",
"redirect_uri" : AppSettings['ruName'],
"scope" : "https://api.ebay.com/oauth/api_scope"
}
data = urllib.parse.urlencode(body)
tokenURL = "https://api.ebay.com/identity/v1/oauth2/token"
response = requests.post(tokenURL, headers=headers, data=data)
return response.json()
response = getAuthToken()
print(response)
response['access_token'] #access keys as required
response['error_description'] #if errors
发布于 2020-10-25 21:13:50
@sunny babau我和你有同样的问题。它确实是由b‘和a拖尾造成的。在上面的代码中添加了以下代码行(删除了这些字符)后,它对我起作用了:
encodedAuthHeader = str(encodedAuthHeader)[2:len(str(encodedAuthHeader))-1]
https://stackoverflow.com/questions/44046855
复制相似问题