Job*_*obs 3 python django oauth ebay-api
我整个星期的大部分时间都在从事ebaySDK的工作。我设法将Trading和Shopping API集成到我的项目中。对于交易API,我使用的是Auth n Auth令牌,其有效期最长为18个月。我需要Sell API的OAuth令牌仅在一天内有效,因此我需要在其过期之前定期提取该令牌。我遵循了网站上的文档,甚至尝试浏览github上的python repos,但到目前为止我还无法继续前进。这是我的请求代码的简短片段,我做错了什么?
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
Run Code Online (Sandbox Code Playgroud)
我得到的答复是:
{u'error_description': u'client authentication failed', u'error': u'invalid_client'}
Run Code Online (Sandbox Code Playgroud)
我检查了所有的钥匙。我什至试图通过ebay提供的生产登录来获取令牌,但无济于事。我从ebay提供的网址获得的响应是html和js代码(无JSON或任何数据)。
有人遇到过类似的问题吗?我该如何处理?我提出的要求有误吗?非常感谢任何见解
小智 7
因为这是遍历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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1823 次 |
| 最近记录: |