【发布时间】:2021-02-14 19:22:33
【问题描述】:
我收到 {"error":"invalid_client","error_description":"client authentication failed"} 401 响应。 我确实设法通过手动访问网址并登录到我的帐户来获得用户同意(文档:https://developer.ebay.com/api-docs/static/oauth-authorization-code-grant.html):
import requests, urllib, base64
my_AppID = "someAppID"
my_Ru_Name = "someRuName"
scope = "https://api.ebay.com/oauth/api_scope/sell.fulfillment"
scope = urllib.parse.quote_plus(scope)
url = f"""https://auth.ebay.com/oauth2/authorize?
client_id={my_AppID}&
redirect_uri={my_Ru_Name}&
response_type=code&
scope={scope}&"""
我打印了 url 字符串并在浏览器中访问它,登录并“同意”。 此页面显示“授权成功完成”。所以我从新的重定向页面 url 中获取了代码值。 在此之后,我无法将授权代码交换为用户访问令牌:
my_CertID = "someCertID"
client_id = base64.b64encode(my_AppID.encode())
client_secret = base64.b64encode(my_CertID.encode())
auth_string = "Basic " + client_id.decode() + ":" + client_secret.decode()
consent_code = "v%521.1%25i..................jYw" # from the page's link after logging in
consent_code = urllib.parse.quote_plus(code)
headers = {"Content-Type": "application/x-www-form-urlencoded", "Authorization": auth_string}
data = {"grant_type": "authorization_code", "code": consent_code , "redirect_uri": Ru_Name}
url_token = "https://api.ebay.com/identity/v1/oauth2/token"
resp = requests.post(url_token, headers=headers, data=data)
print(resp.text)
# the response I get:
{"error":"invalid_client","error_description":"client authentication failed"}
我做错了什么? 是请求部分吗?编码?
我对这一切有点陌生,所以提前谢谢!
【问题讨论】:
标签: python-3.x api python-requests ebay-api