2017-08-27 71 views
1

我正尝试使用POST方法向API进行认证。这里是我所指的文件Kite Connect API。 我无法理解我出错的地方。错误与校验和或POST有关吗?使用带响应键和校验和的REST API进行POST认证

library(digest) 
require("httr") 

my_api <- "xxx" 
my_req_token <- 'yyy' 
my_secret <- 'zzz' 

check<-hmac(my_req_token,paste0(paste0(my_api,my_req_token),my_secret),algo=c('sha256')) 

url <- 'https://api.kite.trade/session/token' 
login <- list(api_key=my_api, 
       request_token = my_req_token, 
       checksum = check) 

response<- POST(url,body= login) 

这是我收到的回复。

> response 
Response [https://api.kite.trade/session/token] 
Date: 2017-08-27 12:34 
Status: 400 
Content-Type: application/json 
Size: 81 B 

> content(response, "parsed", "application/json") 
$status 
[1] "error" 

$message 
[1] "Missing api_key" 

$error_type 
[1] "InputException" 

回答

0

问题得到解决。交的数据应被发送为 '应用程序/ x WWW的形式进行了urlencoded',编码ARG设置为形式执行此

library(digest) 
require("httr") 

my_api <- "xxx" 
my_req_token <- 'yyy' 
my_secret <- 'zzz' 

#use digest insted of hmac; digest serializes argument first, use serialize arg to disable that 
check<-digest(paste0(my_api, my_req_token, my_secret), algo='sha256', serialize=FALSE) 

url <- 'https://api.kite.trade/session/token' 
login <- list(api_key=my_api, 
       request_token = my_req_token, 
       checksum = check) 

#post data should be sent as 'application/x-www-form-urlencoded', setting encode arg to form does this 
response<- POST(url,body= login, encode='form') 

parsed_content <- content(response, "parsed", "application/json") 
0

给这个一杆

#devtools::install_github("hrbrmstr/curlconverter") 

library(curlconverter) 

curlExample <- 'curl https://api.kite.trade/session/token 
    -d "api_key=xxx" 
    -d "request_token=yyy" 
    -d "checksum=zzz"' 

resp <- make_req(straighten(curlExample)) 
resp 
+0

'> RESP [[1]] 函数() HTTR :: VERB(动词=“GET”,url =“https://api.kite.trade/session/token”) <环境:0x0000000011ed3618>' –

+0

这是我从代码中得到的回应。 *为校验和添加了hmac –