R:eBay OAuth - 客户端凭证授予请求
R: eBay OAuth - Client Credentials Grant Request
我正在努力了解 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"
正文请求有什么问题,为什么?
我在这里留下一个工作示例。做到这一点非常困难,但我做到了。
首先,我获得了应用程序访问令牌,然后我将其用于搜索 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 编写的代码。祝你有美好的一天。我希望这将是解决方案。
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"]]
我正在努力了解 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"
正文请求有什么问题,为什么?
我在这里留下一个工作示例。做到这一点非常困难,但我做到了。 首先,我获得了应用程序访问令牌,然后我将其用于搜索 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 编写的代码。祝你有美好的一天。我希望这将是解决方案。
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"]]