men*_*shi 18 token access-token ebay-api
使用eBay令牌认证已经挣扎了几天.我发现很难理解如何获取新令牌,在注册开发者计划帐户后,我请求了密钥集并获得了它们,之后我授权访问Auth'n'Auth令牌,该令牌有望持续18个月,是的,令牌仅适用于交易,购物和寻找API.
但是当你需要执行购买,销售和商业API时,你必须获得oauth令牌.您可以执行所谓的"单用户应用程序"样式并从用户令牌工具登录oauth,并获得2小时到期的oauth.
稍后,令牌过期,你有点失去了上面提到的api的访问权限.我尝试从交易>获取会话ID,交易>获取令牌中获取令牌,但在向Fetch令牌提供会话ID后,它说:"最终用户尚未完成Auth&Auth签名流程." 虽然有一个有效的18个月令牌,但它会一直返回此错误.
有没有关于此的任何示例文章,任何人都可能阅读或写过?
Ful*_*ool 37
你的困惑并非毫无根据.我自己对这个API流程的体验,以及大部分官方开发论坛的经验都给人留下了压力.下面详细介绍了生成与您是连接到单个,专用帐户还是多个用户帐户无关的oauth的过程.
有官方指南,它解释了整个过程,所以我在这里重新创建整个指南犹豫不决.我可以提供一个摘要(我建议你在尝试通过你的应用程序之前使用Postman进行以下操作):
https://api.ebay.com/oauth/api_scope/sell.inventory
范围.找出您需要的端点,并转到每个端点的API文档,然后找到范围部分.get请求现在看起来像这样:
`https://signin.sandbox.ebay.com/authorize?
client_id=<your-client-id-value>&
redirect_uri=<your-RuName-value>&
response_type=code&
scope=https%3A%2F%2Fapi.ebay.com%2Foauth%2Fapi_scope%2Fsell.account%20
https%3A%2F%2Fapi.ebay.com%2Foauth%2Fapi_scope%2Fsell.inventory`
Run Code Online (Sandbox Code Playgroud)
还建议您添加一个state
查询字符串,为了便于使用,我省略了该字符串,但您应该研究它们是什么以及为什么建议它们用于OAuth.
code
查询字符串.如果您正在为多个用户开发应用程序并计划让他们在此页面上登录,那么您需要配置您的应用程序以获取确认响应,该响应将是上述URL,并从中提取代码.这段代码非常短暂.如果您通过浏览器手动检索它,则需要快速完成后续步骤.您现在需要对https://api.sandbox.ebay.com/identity/v1/oauth2/token执行POST请求.请参阅以下结构:
HTTP method: POST
URL (Sandbox): https://api.sandbox.ebay.com/identity/v1/oauth2/token
HTTP headers:
Content-Type = application/x-www-form-urlencoded
Authorization = Basic <B64-encoded-oauth-credentials> (A base64-encoded value made from your client ID and client secret, separated by colon. For example, in PHP you could generate it with: `base64_encode ("fakeclientid123:fakeclientsecret123")`)
Request body (wrapped for readability):
grant_type=authorization_code& (literally the string "authorization_code")
code=<authorization-code-value>& (code retreived in previous step)
redirect_uri=<RuName-value> (same RuName as earlier)
Run Code Online (Sandbox Code Playgroud)
如果成功,此请求将返回如下内容:
{
"access_token": "v^1.1#i^1#p^3#r^1...XzMjRV4xMjg0",
"token_type": "User token",
"expires_in": 7200,
"refresh_token": "v^1.1#i^1#p^3#r^1...zYjRV4xMjg0",
"refresh_token_expires_in": 47304000
}
Run Code Online (Sandbox Code Playgroud)
有我们追求的oauth令牌,将持续2个小时.第二个令牌是刷新令牌,持续约18个月.保持此令牌安全,不要共享它,也不要在您的应用程序中对其进行硬编码.从此时起,您的应用应使用此令牌执行刷新调用,以便在需要时获取新的oauth.一旦18个月启动,或者用户再次执行"允许访问"过程,您将需要执行上述所有操作以生成新的刷新令牌.假设API没有改变那一点.
值得注意的是,18个月的生命周期不是OAuth刷新的正常过程,通常每次使用旧刷新令牌时都会返回一个新的刷新令牌.
刷新oauth:
HTTP method: POST
URL (Sandbox): https://api.sandbox.ebay.com/identity/v1/oauth2/token
HTTP headers:
Content-Type = application/x-www-form-urlencoded
Authorization = Basic <B64-encoded-oauth-credentials>
Request body (wrapped for readability):
grant_type=refresh_token&
refresh_token=<your-refresh-token-value>&
scope=https%3A%2F%2Fapi.ebay.com%2Foauth%2Fapi_scope%2Fsell.account%20
https%3A%2F%2Fapi.ebay.com%2Foauth%2Fapi_scope%2Fsell.inventory
Run Code Online (Sandbox Code Playgroud)我希望这有帮助!