我正在尝试使用POST方法对API进行身份验证。这是我所指的Kite Connect 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 :(得分: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
答案 1 :(得分:0)
问题已解决。发布数据应该以'application / x-www-form-urlencoded'的形式发送,将encode arg设置为form for this
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")