Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an Ebay OAuth Token

I've been working on the ebaySDK for most of the week. I've managed to integrate the Trading and Shopping APIs into my project. For the trading API, I was using an Auth n Auth token which has a validity of upto 18 months. The OAuth Token which I need for the Sell APIs is valid only for a day, so I'll need to fetch it regularly before it expires. I followed the documentation on the site and even tried looking through python repos on github but I haven't been able to move forward as of yet. Here's a quick snippet of my request code, What am I doing wrong?

import requests, json, base64, xmltodict

AppSettings = {
    'app_id' : 'my_app_id',
    'app_secret' : 'my_app_secret',
    'dev_id': 'my_dev_id',
    'ruName': 'the_ruName_for_my_app'
}
authHeaderData =  AppSettings['app_id']+':'+AppSettings['app_secret']
encodedAuthHeader = base64.b64encode(authHeaderData)

session = requests.Session()

print encodedAuthHeader 
url = 'https://api.ebay.com/identity/v1/oauth2/token'

session.headers.update({
    'Content-Type':'application/x-www-form-urlencoded',
    'Authorization':'Basic '+encodedAuthHeader
    })

data = {
    'grant_type':'client_credentials',
    'redirect_uri': AppSettings['ruName'],
    'scope':'https://api.ebay.com/oauth/api_scope'
}

response = session.post(url, data=data).json()
print response

The response I'm getting is:

{u'error_description': u'client authentication failed', u'error': u'invalid_client'}

I checked all the keys. I even tried to get the token via the production signin provided by ebay but to no avail. The response I got from the url provided by ebay was html and js code (No JSON or any data).

Has anybody faced a similar issue? How can I work through this? Am I making the requests wrong? Any insight is greatly appreciated

like image 574
Jobs Avatar asked May 18 '17 11:05

Jobs


People also ask

How do I get my eBay refresh token?

To get a refresh token, you must mint a new User access token. This process starts with Getting the user's consent. Set scope to the same URL-encoded list of scopes that you used in the original consent request. For details on scopes, see Using OAuth to access eBay APIs.


2 Answers

Because this was such a nightmare to traverse eBays docs to find this answer, I figured i would post my function that solved this.

import requests, urllib, base64

def getAuthToken():
     AppSettings = {
          'client_id':'<client_id>',
          'client_secret':'<client_secret>',
          'ruName':'<ruName>'}

     authHeaderData = AppSettings['client_id'] + ':' + AppSettings['client_secret']
     encodedAuthHeader = base64.b64encode(str.encode(authHeaderData))

     headers = {
          "Content-Type" : "application/x-www-form-urlencoded", 
          "Authorization" : "Basic " + str(encodedAuthHeader)
          }

     body= {
          "grant_type" : "client_credentials",
          "redirect_uri" : AppSettings['ruName'],
          "scope" : "https://api.ebay.com/oauth/api_scope"
      }

     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
like image 91
RyanPPG Avatar answered Oct 10 '22 22:10

RyanPPG


@sunny babau I was having the same problem as you. It was indeed caused by the b' and a trailing '. After adding the following line to the code above, which removes these characters, it worked for me:

encodedAuthHeader = str(encodedAuthHeader)[2:len(str(encodedAuthHeader))-1]
like image 27
Edwin van Assen Avatar answered Oct 10 '22 22:10

Edwin van Assen