将ebay Feed API与R结合使用

问题描述 投票:1回答:2

[我正在尝试使用R调用ebay Feed API,但不了解所使用的语法,似乎我在API调用中缺少标题:

> res <- GET(paste0("https://api.ebay.com/buy/feed/v1_beta/item?feed_scope=ALL_ACTIVE&category_id=625&date=20191216"))
> res
Response [https://api.ebay.com/buy/feed/v1_beta/item?feed_scope=ALL_ACTIVE&category_id=625&date=20191216]
  Date: 2019-12-21 08:39
  Status: 400
  Content-Type: application/json
  Size: 228 B
{
  "errors" : [ {
    "errorId" : 1002,
    "domain" : "OAuth",
    "category" : "REQUEST",
    "message" : "Missing access token",
    "longMessage" : "Access token is missing in the Authorization HTTP request header."
  } 

标题应该在哪里?我已经看到了类似的事情,添加了参数,这会正确吗? payload是不同身份验证元素的列表:

res_bis <- GET("https://api.ebay.com/buy/feed/v1_beta/item?feed_scope=ALL_ACTIVE&category_id=625&date=20191216", authenticate() = payload, encode = "form", verbose())

非常感谢!

EDIT1:我看到了有关HTTP标头的信息:

HTTP请求标头:Content-Type –必须设置发送至:application / x-www-form-urlencoded授权-“基本”一词”,然后是您的Base64编码的OAuth凭据(client_id:client_secret)。

然后我尝试了以下操作,但仍然出现相同的错误:

 GET("https://api.ebay.com/buy/feed/v1_beta/item?feed_scope=ALL_ACTIVE&category_id=625&date=20191216", add_headers("Basic client_id:client_secret"))

EDIT2:在Andrea的帮助下更新我的代码:

> GET("https://api.ebay.com/buy/feed/v1_beta/item?feed_scope=ALL_ACTIVE&category_id=625&date=20191216", 
+     add_headers(client_id = paste0("Basic", " ",your_token)), content_type("application/x-www-form-urlencoded")  )

Response [https://api.ebay.com/buy/feed/v1_beta/item?feed_scope=ALL_ACTIVE&category_id=625&date=20191216]
  Date: 2019-12-21 12:17
  Status: 400
  Content-Type: application/json
  Size: 228 B
{
  "errors" : [ {
    "errorId" : 1002,
    "domain" : "OAuth",
    "category" : "REQUEST",
    "message" : "Missing access token",
    "longMessage" : "Access token is missing in the Authorization HTTP request header."
  } ]
> 

EDIT3:多亏了Andrea,我设法获得了access tokenenter image description here

但是我仍然会遇到相同的错误:

your_token= "XXXXXXXXX"
GET("https://api.ebay.com/buy/feed/v1_beta/item?feed_scope=ALL_ACTIVE&category_id=625&date=20191216", add_headers(client_id = paste0("Basic", " ",your_token)), content_type("application/x-www-form-urlencoded")  )

Response [https://api.ebay.com/buy/feed/v1_beta/item?feed_scope=ALL_ACTIVE&category_id=625&date=20191216]
  Date: 2019-12-22 17:56
  Status: 400
  Content-Type: application/json
  Size: 228 B
{
  "errors" : [ {
    "errorId" : 1002,
    "domain" : "OAuth",
    "category" : "REQUEST",
    "message" : "Missing access token",
    "longMessage" : "Access token is missing in the Authorization HTTP request header."
  } ]
r rest api get ebay-api
2个回答
0
投票

感谢您的耐心安德里亚,您的最后建议似乎正在奏效!好吧,我没有得到预期的数据(从Ebay获得了403状态代码错误,而从Ebay API得到了很多空字段),但是在调查之后,我认为这与我的Ebay应用尚未得到Ebay授权有关。希望我能写这篇文章,谢谢您的宝贵时间!


0
投票

基于上述交流,主要问题是对ebay feed api auth flows的理解不足。首先,必须获取一个Authorization令牌来认证将来的api请求。

library(httr)
library(jsonify)

api_token <- "your_token_string"

# Get authorization token
auth_token_res <- GET("https://api.sandbox.ebay.com/identity/v1/oauth2/token",
                      add_headers(client_id = paste0("Basic", " ",api_token)),
                      content_type("application/x-www-form-urlencoded")) %>% 
  fromJSON()

access_token <- auth_token_res[["access_token"]]  # parse it from JSON resp

该令牌将像以前一样在以后的呼叫中以add_headers()传递:

# Make request
feed_res <- GET("https://api.ebay.com/buy/feed/v1_beta/item?feed_scope=ALL_ACTIVE&category_id=625&date=20191216",
                add_headers(Authorization = paste0("Bearer", " ",access_token)))

# ... parse fields as needed from JSON  response
© www.soinside.com 2019 - 2024. All rights reserved.