Ebay OAuth API ERROR: invalid_grant - the provided authorization grant code is invalid or was issued to another client
Asked Answered
C

1

7

I am trying to get a OAuth User Access Token from the eBay API using this document: https://developer.ebay.com/api-docs/static/oauth-authorization-code-grant.html

My Code works perfectly fine if I use: "grant_type": "client_credentials"

but when I call: "grant_type": "authorization_code" I get this error:

{'error': 'invalid_grant', 'error_description': 'the provided authorization grant code is invalid or was issued to another client'}

Here is my code:

    # 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)

My brain hurts from re-reading all the manuals and search results. Please help!

Commix answered 6/3, 2021 at 8:32 Comment(0)
C
10

I solved it!!!!

Basically when you authorize your app it redirects you to the redirect_uri aka (RuName). The difference with the "grant_type": "authorization_code" is that it returns a far more complicated string which comes back as URL encoded format. This is where special characters get encoded as URL friendly characters

Example: quote('/El Niño/') yields '/El%20Ni%C3%B1o/'

The returned code from ebay looks like this:

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

I undid the URL formatting using this: https://docs.python.org/3/library/urllib.parse.html

from urllib.parse import unquote

... and Added the unquote function to decode the URL from ebay back to normal

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

This makes the authorization_code look like it should: code=v^1.1#i^1#I^3Af^0#p^3#r^1#t^Ul32XzE6M0RBRDYyQTk4RTg5NTcwNzdFODVGQjFFN0MzQjMyMTRfMxI0VeMTI4NA==

and now I have my Tokens!!!!!!!

Commix answered 6/3, 2021 at 10:38 Comment(1)
btw, unquote seems like it's similar to JavaScripts decodeURIComponent, which might be easier to use with just the dev consoleErg

© 2022 - 2024 — McMap. All rights reserved.