【问题标题】:Ebay OAuth API ERROR: invalid_grant - the provided authorization grant code is invalid or was issued to another clientEbay OAuth API 错误:invalid_grant - 提供的授权授权代码无效或已颁发给另一个客户端
【发布时间】:2021-03-06 08:32:57
【问题描述】:

我正在尝试使用此文档从 eBay API 获取 OAuth 用户访问令牌: https://developer.ebay.com/api-docs/static/oauth-authorization-code-grant.html

如果我使用以下代码,我的代码可以正常工作:"grant_type": "client_credentials"

但是当我打电话时:"grant_type": "authorization_code" 我得到这个错误:

{'error': 'invalid_grant', 'error_description': '提供的授权授权码无效或已发给其他客户端'}

这是我的代码:

    # 1. Send a user to authorize your app
    auth_url = ('https://auth.sandbox.ebay.com/oauth2/authorize?client_id=Software-Software-SBX-2dc485992e-65aa6c75&response_type=code&redirect_uri=Soft_ware_-Software-Softwa-zwdqqdbxx&scope=https://api.ebay.com/oauth/api_scope https://api.ebay.com/oauth/api_scope/buy.order.readonly https://api.ebay.com/oauth/api_scope/buy.guest.order https://api.ebay.com/oauth/api_scope/sell.marketing.readonly https://api.ebay.com/oauth/api_scope/sell.marketing https://api.ebay.com/oauth/api_scope/sell.inventory.readonly https://api.ebay.com/oauth/api_scope/sell.inventory https://api.ebay.com/oauth/api_scope/sell.account.readonly https://api.ebay.com/oauth/api_scope/sell.account https://api.ebay.com/oauth/api_scope/sell.fulfillment.readonly https://api.ebay.com/oauth/api_scope/sell.fulfillment https://api.ebay.com/oauth/api_scope/sell.analytics.readonly https://api.ebay.com/oauth/api_scope/sell.marketplace.insights.readonly https://api.ebay.com/oauth/api_scope/commerce.catalog.readonly https://api.ebay.com/oauth/api_scope/buy.shopping.cart https://api.ebay.com/oauth/api_scope/buy.offer.auction https://api.ebay.com/oauth/api_scope/commerce.identity.readonly https://api.ebay.com/oauth/api_scope/commerce.identity.email.readonly https://api.ebay.com/oauth/api_scope/commerce.identity.phone.readonly https://api.ebay.com/oauth/api_scope/commerce.identity.address.readonly https://api.ebay.com/oauth/api_scope/commerce.identity.name.readonly https://api.ebay.com/oauth/api_scope/commerce.identity.status.readonly https://api.ebay.com/oauth/api_scope/sell.finances https://api.ebay.com/oauth/api_scope/sell.item.draft https://api.ebay.com/oauth/api_scope/sell.payment.dispute https://api.ebay.com/oauth/api_scope/sell.item https://api.ebay.com/oauth/api_scope/sell.reputation https://api.ebay.com/oauth/api_scope/sell.reputation.readonly')

    webbrowser.open_new(auth_url)

    # 2. Users are redirected back to you with a code

    url = input('enter url: ')
    start_number = auth_res_url.find('code=') + len('code=')
    end_number = auth_res_url.find('&expires_in')
    auth_code = auth_res_url[start_number:end_number]

    # 3. Exchange the code

    with open(r'ebay.yaml') as file:
        config = yaml.load(file, Loader=yaml.FullLoader)

    b64_id_secret = base64.b64encode(bytes(config['api.sandbox.ebay.com']['appid'] + ':' + config['api.sandbox.ebay.com']['certid'], 'utf-8')).decode('utf-8')

    response = requests.post("https://api.sandbox.ebay.com/identity/v1/oauth2/token",
                             headers={
                                      "Content-Type": "application/x-www-form-urlencoded",
                                      "Authorization": "Basic " + b64_id_secret
                                     },
                             data={
                                      "grant_type": "authorization_code",
                                      "code": auth_code,
                                      "redirect_uri": 'XXXXX HIDDEN XXXXX'
                                  })
    token = response.json()
    print(token)

重新阅读所有手册和搜索结果,我的大脑很疼。请帮忙!

【问题讨论】:

    标签: python oauth-2.0 ebay-api ebay-sdk


    【解决方案1】:

    我解决了!!!!

    基本上,当您授权您的应用程序时,它会将您重定向到 redirect_uri aka (RuName)。 与"grant_type": "authorization_code" 的不同之处在于它返回一个更复杂的字符串,该字符串以URL 编码格式 的形式返回。这是特殊字符被编码为 URL 友好字符的地方

    示例:quote('/El Niño/') 产生 '/El%20Ni%C3%B1o/'

    ebay 返回的代码如下所示:

    code=v%5E1.1%23i%5E1%23I%5E3%41f%5E0%23p%5E3%23r%5E1%23t%5EUl32XzE6M0RBRDYyQTk4RTg5NTcwNzdFODVGQjFFN0MzQjMyMTRfMxI0VeMTI4NA%3D%3D

    我使用以下方法取消了 URL 格式:https://docs.python.org/3/library/urllib.parse.html

    from urllib.parse import unquote
    

    ...并添加了取消引用功能以将来自 ebay 的 URL 解码恢复正常

    url = input('enter url: ')
    url = unquote(url)
    

    这使得授权码看起来应该是这样的: code=v^1.1#i^1#I^3Af^0#p^3#r^1#t^Ul32XzE6M0RBRDYyQTk4RTg5NTcwNzdFODVGQjFFN0MzQjMyMTRfMxI0VeMTI4NA==

    现在我有我的代币了!!!!!!!

    【讨论】:

    • 顺便说一句,unquote 好像和 JavaScripts decodeURIComponent 很相似,只用开发控制台可能更容易使用
    猜你喜欢
    • 2018-08-22
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 2017-03-20
    • 1970-01-01
    • 2013-08-03
    • 2017-02-22
    • 1970-01-01
    相关资源
    最近更新 更多