将 ebay Feed API 与 R 一起使用

Using ebay Feed API with R

我正在尝试使用 R 调用 ebay Feed API 但不理解要使用的语法,似乎我在调用 [=44] 时缺少 headers =]:

> 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."
  } 

headers 应该在哪里? 我见过这样的东西,添加参数,那会不会走在正确的道路上? 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 headers:

的信息

HTTP request headers: Content-Type – Must be set to:application/x-www-form-urlencoded Authorization – The word "Basic " followed by your Base64-encoded OAuth credentials(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:
感谢安德里亚,我设法得到了我的 access token

但我仍然遇到同样的错误:

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."
  } ]

根据我们上面的交流,主要问题是对 ebay 的供给方式缺乏了解 api auth flows。 首先,有必要获取授权令牌以验证未来的 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