【问题标题】:R: eBay OAuth - Client Credentials Grant RequestR:eBay OAuth - 客户凭证授予请求
【发布时间】:2020-04-25 19:06:24
【问题描述】:

我正在尝试了解 OAuth,并决定使用 eBay API。在关注他们的 instructions 以获取应用程序访问令牌时,我收到了 400 错误

library(jsonlite)
library(httr)

# OAuth credentials
client_id <- "x"
client_secret <- "x"
# Required - https://developer.ebay.com/api-docs/static/oauth-base64-credentials.html
encod_oauth <- base64_enc(paste0(client_id, ":", client_secret))

auth_token_res <- 
  POST("https://api.sandbox.ebay.com/identity/v1/oauth2/token",
       add_headers("Content-Type" = "application/x-www-form-urlencoded",
                   Authorization = paste0("Basic ", encod_oauth)),
       body = list(grant_type = "client_credentials", 
                   scope = urlEncode("https://api.ebay.com/oauth/api_scope", reserved = T)))

检查内容,这显然与正文中的grant_type有关。

content(auth_token_res)
$error_description
[1] "grant type in request is not supported by the authorization server"

正文请求有什么问题,为什么?

【问题讨论】:

  • 都试过了,都没有成功,谢谢。
  • content(auth_token_res) 抛出同样的错误。
  • 也尝试了 cURL,但出现错误:{"error":"invalid_request","error_description":"request is missing a required parameter or malformed."}(base)。认为联系支持可能是最好的选择。
  • 可能API手册没有更新

标签: r post oauth-2.0 ebay-api httr


【解决方案1】:

花了一个半小时,但我做到了。您可以在下面找到用 R 编写的代码。祝您有美好的一天。我希望这将是解决方案。

library(httr)
library(jsonlite)
library(base64enc)

rm(list=ls()) # delete memory
code <- base64encode(charToRaw("TT"));

#getting data from api 
resp <- POST("https://api.ebay.com/identity/v1/oauth2/token",
             query = list(grant_type="client_credentials",scope="https://api.ebay.com/oauth/api_scope"),
            add_headers(`Content-Type`='application/x-www-form-urlencoded',
                        `Authorization`=paste("Basic",code, sep=" ")))

if (http_type(resp) != "application/json") {
  stop("API did not return json", call. = FALSE)
}
parsed <- jsonlite::fromJSON(content(resp, "text",encoding = "UTF-8"), simplifyVector = TRUE)
parsed_v2 <- parsed[["response"]]

【讨论】:

    【解决方案2】:

    我在这里留下一个工作示例。这样做非常困难,但我做到了。 首先我得到应用程序访问令牌,然后我将它用于搜索 api。是node.js代码。

    var axios = require('axios');
    var qs = require("querystring");
    //The client credentials grant flow
    //https://developer.ebay.com/api-docs/static/oauth-client-credentials-grant.html
    
    axios("https://api.ebay.com/identity/v1/oauth2/token", {
        method: "POST",
        headers: {
            "Content-Type": "application/x-www-form-urlencoded",
            "Authorization": "Basic " + Buffer.from(
                //App ID (Client ID):
                `your id and secret key here with : seperated`
            ).toString('base64')
        },
        data: qs.stringify({
            grant_type: "client_credentials",
            scope: "https://api.ebay.com/oauth/api_scope",
            // parsed from redirect URI after returning from eBay,
    
        })
    })
        .then(response => {
            console.log("Application data");
            console.log(response.data.access_token);
    
            axios("https://api.ebay.com/buy/browse/v1/item_summary/search", {
                method: "GET",
                headers: {
                    Authorization: "Bearer " + response.data.access_token
                },
                params: {
                    category_ids: "108765",
                    q: "Beatles"
                }
            })
                .then(res => {
                    // console.log(res.data);
                    res.data.itemSummaries.map((e) => console.log(e.title));
                })
                .catch(err => {
                    console.log("**************Get search error**************")
                    console.log(err)
                });
        })
        .catch(err => {
            console.log("**************Get access token error**************")
            console.log(err)
        });
    

    【讨论】:

    • 我遇到了关于用户访问令牌的问题。如果您已经解决了这个问题并获得了用户访问令牌。我们将共同努力。这是一个非常令人不安的过程。
    • 无法真正将其标记为完整,因为我正在寻找 R 答案。话虽如此,这里的任何语言都很少,所以希望这对其他人有用。
    • 如果我有时间我会用 R 做它并从这里分享。你看一个R解决方案是对的。这个比较好。实际上,当我编写此解决方案时,我认为没有人回复或查看 5 个月前的问题 :) 我正在寻找用户访问令牌解决方案,但发现此问题尚未得到任何人的答复。我想写。
    猜你喜欢
    • 2019-12-03
    • 2015-12-06
    • 1970-01-01
    • 2018-02-14
    • 1970-01-01
    • 2019-08-09
    • 2020-11-18
    • 2018-05-24
    • 2017-03-11
    相关资源
    最近更新 更多