【问题标题】:POST authentication using REST API with response key and checksum使用带有响应密钥和校验和的 REST API 进行 POST 身份验证
【发布时间】:2017-08-27 13:04:18
【问题描述】:

我正在尝试使用 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"

【问题讨论】:

    标签: r rest authentication http-post checksum


    【解决方案1】:

    试一试

    #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
    

    【讨论】:

    • &gt; resp [[1]] function () httr::VERB(verb = "GET", url = "https://api.kite.trade/session/token") &lt;environment: 0x0000000011ed3618&gt;
    • 这是我从代码中得到的响应。 *为校验和添加了hmac
    【解决方案2】:

    问题已解决。发布数据应作为 'application/x-www-form-urlencoded' 发送,将编码 arg 设置为 form 这样做

    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")
    

    【讨论】:

      猜你喜欢
      • 2011-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-22
      • 2014-12-17
      • 1970-01-01
      相关资源
      最近更新 更多