Getting an Ebay OAuth Token

2020-03-24 08:40发布

问题:

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

回答1:

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