获取Ebay OAuth令牌 [英] Getting an Ebay OAuth Token

查看:260
本文介绍了获取Ebay OAuth令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

整个星期的大部分时间里,我一直在使用ebaySDK。我设法将Trading和Shopping API集成到我的项目中。对于交易API,我使用的是Auth n Auth令牌,其有效期最长为18个月。我需要Sell API的OAuth令牌仅在一天内有效,因此,我需要在其过期之前定期提取该令牌。
我遵循了网站上的文档,甚至尝试浏览github上的python repos,但到目前为止我还无法继续前进。这是我的请求代码的简短片段,我做错了什么?

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

我得到的答复是:

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

我检查了所有键。我什至试图通过ebay提供的生产登录来获取令牌,但无济于事。我从ebay提供的网址获得的响应是​​html和js代码(无JSON或任何数据)。

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

推荐答案

因为这真是一场噩梦,遍历eBays文档来找到这个答案,所以我想我会发布

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

这篇关于获取Ebay OAuth令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆