我在通过0auth访问跟踪API时遇到了一些问题;
我有签名密钥,密码和Authorization_key。
签名密钥和秘密应该形成我理解的sig文件和应该作为标题添加的authorization_key。
代码:
auth_info <-
read.csv2('./Data/auth_info.csv',
encoding = 'UTF-8',
stringsAsFactors = F)
express <-
oauth_app("express", key = 'Ur1475', secret = auth_info$sign_key)
sig <- sign_oauth1.0(express)
order <-
GET(
"http://www.express.ru/api/v2/getOrder?orderNumber=WEBN3141018",
authenticate("Ur1475", auth_info$sign_key),
add_headers(Authorization = auth_info$auth_key),
sig
)
order_info <- content(order)
order_info
返回我的身份验证错误;
官方api文档告诉应该形成md5(客户端密钥.URL .GET参数.POST params。签名密钥)。
此外,我还查看了不使用http的0auth功能代码。 它会导致问题吗?
请您评论如何通过R从API获取数据?
API doc(俄语)):https://www.express.ru/docs/APIExpressRu.pdf
答案 0 :(得分:0)
API的主要部分(Google,Twitter,Facebook)需要您必须从oauth身份验证中获取的令牌。根据API,您必须使用函数{
"current_page":1,
"data":[
{
"id":2,
"subject":"demo link data",
"quotesum": 1260
},
{
"id":1,
"subject":"demo",
"quotesum": 360
}
],
"per_page":10,
"prev_page_url":null,
"to":2,
"total":2
}
或GET
。您必须提供以POST
编码的密钥。所以我认为你需要的是:
base64
if(!require("jsonlite")){library("jsonlite")}
if(!require("httr")){library("httr")}
#Create your own appication key
consumer_key <- "your consumer key" #needed
consumer_secret <- "your consumer secret" #some api's don't need.
#Use basic auth
secret <- jsonlite::base64_enc(paste(consumer_key, consumer_secret, sep = ":"))
req <- httr::POST("https://url.of.your.api/oauth/token", #"oauth" and "token" are not parameters, they are fixed.
httr::add_headers(
"Authorization" = paste("Basic", gsub("\n", "", secret)),
"Content-Type" = "application/x-www-form-urlencoded;charset=UTF-8"
),
body = "grant_type=client_credentials"
)
#Extract the access token
token <- paste("Bearer", httr::content(req)$access_token)
正如我所说,某些API需要url<-"http://url.of.your.api/search?parameter1=value¶meter2=value"
req <- httr::POST(url, httr::add_headers("Authorization" = token))
json <- httr::content(req)
json
和其他GET
,请同时尝试。