问题描述
我正在尝试访问GetDealItems API,但我梦a以求地开始工作。即使我使用有效的 client_id','client_secret','ruName'
我仍然得到
I'm trying to access GetDealItems API and i have a nightmare to get this working. Even though I use the valid client_id','client_secret','ruName'
i keep getting
{'error': 'invalid_client', 'error_description': 'client authentication failed'}
下面是eBay doc
below is the ebay doc
https://developer.ebay.com/api-docs/buy/deal/resources/deal_item/methods/getDealItems
我想我需要在请求中使用此范围和网址
I guess i need to use this scope and url in my request
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代码。
Please see below my Python code.
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
推荐答案
我看到的最明显的问题是您使用的是 Bearer
应该在授权
标头中使用 Basic
。
The most obvious problem I see is that you are using Bearer
when you should be using Basic
in your Authorization
header.
也,当您将整个字典传递到 urlencode
时,您正在对 redirect_url
进行编码。文档说您应该对 scope
参数进行urlencode,但是老实说,我从未对范围进行编码,它仍然对我有用。
Also, You are urlencoding your redirect_url
when you pass the entire dictionary into urlencode
. The docs say you are supposed to urlencode the scope
parameter, but honestly, I never encode the scope and it still works for me.
这是修改后的代码,但有一些格式更改:
Here is your modified code, with a few formatting changes:
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()
更新:
我认为您需要使用 而不是 client_credentials
。
Update:
I think you need to use the authorization_code
grant instead of client_credentials
.
要使用 authorization_code
补助金,请修改您的身体,使其看起来像这样:
To use the authorization_code
grant, modify your body to look like this:
data = (
'grant_type=authorization_code&'
f'code={authorization_code}&'
f'redirect_uri={ruName}&'
f'scope={scope}'
)
此外,您将需要遵循重定向网址 以获得实际的授权代码。执行以下操作:
Also, you will need to follow your "redirect url" to get the actual authorization code. Execute the following:
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)
从stdout复制/粘贴url,单击链接,然后单击接受,那么您将被重定向到如下所示的网址:
Copy/paste the url from stdout, follow the link, and click "accept", then you will be redirected to a url that looks like this:
https://signin.ebay.com/ws/eBayISAPI.dll?ThirdPartyAuthSucessFailure&isAuthSuccessful=true&code=<authorization code here>&expires_in=299
复制/将授权代码粘贴到您的代码中,然后查看它是否有效。
Copy/paste the authorization code into your code, then see if it works.
实际上,eBay希望您使用服务器在应用程序中自动执行此操作,但这样做对您没有意义。如果您要构建供个人使用的应用程序,那就麻烦了。
Realistically, eBay expects you to automate this within your application using a server, but it doesn't make sense for you to go through the trouble if you are building an app for personal use.
这篇关于Ebay令牌和GetDealItems API调用问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!