简体   繁体   English

将 ebay Feed API 与 R 结合使用

[英]Using ebay Feed API with R

I am trying to call the ebay Feed API using R but don't understand the syntax to be used, seems like I am missing the headers in the call to the API:我正在尝试使用 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."
  } 

Where should be the headers?标题应该在哪里? I have seen something like this, adding parameters, would that be going on the right path?我见过这样的事情,添加参数,这会走在正确的道路上吗? payload being a list of different authentication elements: 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())

Help much appreciated!非常感谢帮助!

EDIT1 :编辑1
I saw an information about HTTP headers:我看到了一个关于 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). HTTP 请求标头: Content-Type – 必须设置为:application/x-www-form-urlencoded Authorization – 单词“Basic”后跟 Base64 编码的 OAuth 凭据(client_id:client_secret)。

I then tried the following but still got same error:然后我尝试了以下但仍然得到同样的错误:

 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 : Updating my code following Andrea's help: 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 :编辑3
Thanks to Andrea I managed to get my access token :感谢 Andrea,我设法获得了我的access token 在此处输入图片说明

But I still get the same error when I do:但是当我这样做时,我仍然遇到同样的错误:

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

Based on our exchange above, the main issue was a lack of understanding of how ebay feed api auth flows .根据我们上面的交流,主要问题是缺乏对 ebay feed api auth 流程的了解。 First, it is necessary to acquire an Authorization token to authenthicate future api requests.首先,需要获取一个授权令牌来验证未来的 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

That token will be then passed in future calls with add_headers() just like before:该令牌将在以后的调用中像以前一样通过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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM