我正在尝试访问 GetDealItems API,但我很难让它正常工作。即使我使用有效的 client_id','client_secret','ruName'
我仍然得到
{'error': 'invalid_client', 'error_description': 'client authentication failed'}
下面是 eBay 文档
https://developer.ebay.com/api-docs/buy/deal/resources/deal_item/methods/getDealItems
我想我需要在我的请求中使用这个范围和网址
scopes:'https://api.ebay.com/oauth/api_scope/buy.deal' and the
url='https://api.ebay.com/buy/deal/v1/deal_item?limit=1000'
请参阅下面我的 Python 代码。
import requests, urllib, base64
def getAuthToken():
AppSettings = {
'client_id':'xxxx7c8ec878c-c80c4c69',
'client_secret':'xxxx56db-4b4a-97b4-fad2',
'ruName':'xxxxx-gscrcsrtj'}
authHeaderData = AppSettings['client_id'] + ':' + AppSettings['client_secret']
encodedAuthHeader = base64.b64encode(str.encode(authHeaderData))
headers = {
"Content-Type" : "application/x-www-form-urlencoded",
"Authorization" : "Bearer " + str(encodedAuthHeader)
}
body= {
"grant_type" : "client_credentials",
"redirect_uri" : AppSettings['ruName'],
"scope" : "https://api.ebay.com/oauth/api_scope/buy.deal"
}
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
最佳答案
我看到的最明显的问题是,当您应该在 Authorization
header 中使用 Basic
时,您却使用了 Bearer
。
此外,当您将整个字典传递到 urlencode
时,您正在对 redirect_url
进行 urlencode 编码。文档说你应该对 scope
参数进行 urlencode,但老实说,我从未对范围进行编码,它仍然对我有用。
这是您修改后的代码,进行了一些格式更改:
import requests, urllib, base64
client_id='xxxx7c8ec878c-c80c4c69'
client_secret='xxxx56db-4b4a-97b4-fad2'
ruName='xxxxx-gscrcsrtj'
scope = urllib.parse.quote('https://api.ebay.com/oauth/api_scope/buy.deal')
def basic_token(key, secret):
return 'Basic ' + base64.b64encode((key + ':' + secret).encode()).decode()
def getAuthToken():
headers = {
"Content-Type" : "application/x-www-form-urlencoded",
"Authorization" : basic_token(client_id, client_secret)
}
data = (
'grant_type=client_credentials&'
f'redirect_uri={ruName}&'
f'scope={scope}'
)
tokenURL = "https://api.ebay.com/identity/v1/oauth2/token"
response = requests.post(tokenURL, headers=headers, data=data)
return response.json()
更新:
我认为您需要使用 authorization_code
授予而不是 client_credentials
。
要使用 authorization_code
授权,请将您的正文修改为如下所示:
data = (
'grant_type=authorization_code&'
f'code={authorization_code}&'
f'redirect_uri={ruName}&'
f'scope={scope}'
)
此外,您还需要按照“重定向网址”获取实际的授权代码。执行以下命令:
redirect_url = (
'https://auth.ebay.com/oauth2/authorize?'
f'client_id={client_id}&'
f'response_type=code&'
f'redirect_uri={ruName}&'
f'scope={scope}'
)
print(redirect_url)
从标准输出复制/粘贴网址,点击链接,然后单击“接受”,然后您将被重定向到如下所示的网址:
https://signin.ebay.com/ws/eBayISAPI.dll?ThirdPartyAuthSucessFailure&isAuthSuccessful=true&code=<authorization code here>&expires_in=299
将授权代码复制/粘贴到您的代码中,然后查看它是否有效。
实际上,eBay 希望您使用服务器在应用程序中自动执行此操作,但如果您正在构建供个人使用的应用程序,那么经历这些麻烦是没有意义的。
关于python - Ebay token 和 GetDealItems API 调用问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63063985/