【问题标题】:Using ebay Feed API with R将 ebay Feed API 与 R 一起使用
【发布时间】:2019-12-21 09:17:16
【问题描述】:

我正在尝试使用 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 headers的信息:

HTTP 请求标头:Content-Type – 必须设置 to: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
感谢安德里亚,我设法得到了我的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."
  } ]

【问题讨论】:

  • 我可能会选择 add_headers() 并将令牌作为参数传递给该函数。
  • 谢谢,你是对的。让我用更多信息编辑我的帖子。
  • 试试add_headers(content_type("application/x-www-form-urlencoded"), client_id = paste0("Basic", " ",your_token)
  • 我的错,不能再编辑评论了。 content_type() 调用应该放在主要的GET() 调用中。
  • 别担心,以前去过。试试GET("https://api.ebay.com/buy/feed/v1_beta/item?feed_scope=ALL_ACTIVE&amp;category_id=625&amp;date=20191216", add_headers(Authorization = paste0("Bearer", " ",your_token))) 之类的东西。在 this 这个页面,您可以找到更多关于 Authorization 标头的信息。

标签: r rest api get ebay-api


【解决方案1】:

根据我们上面的交流,主要问题是对 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    相关资源
    最近更新 更多