在API Request- R中添加“ nonce”

时间:2019-02-09 12:26:42

标签: r nonce

我正在尝试在R中为需要身份验证和随机数的交换平台Exmo建立一个请求。(https://exmo.com/en/api#/authenticated_api)我尝试了几种代码,但始终遇到相同的错误。

我的代码是:

query <-    
  list(    
    nonce = as.numeric(as.POSIXct(Sys.time()))    
  )    

signature <-    
    digest::hmac(    
    key = "secretkey",      
    object = paste(names(query), query, sep = "=", collapse = "&"),        
    algo = "sha512"           
  )        

(acc <- POST(        
  url= "https://api.exmo.com/v1/user_info/",        
  add_headers(Key = "publickey"),        
  add_headers(Sign = "secret key"),        
  add_headers(nonce=nonce),        
  query = c(signature = signature),        
  verbose()        
))        

library(jsonlite)        
get_exmo<- content(acc, as="text")      # Convert to "character"        
(get_exmo_json <- fromJSON(get_exmo)) 

我得到的错误是:

$ result [1]虚假

$ error [1]“ 40007:格式参数“ nonce”不正确”

任何想法如何添加正确的随机数格式?

2 个答案:

答案 0 :(得分:0)

https://github.com/exmo-dev/exmo_api_lib/blob/master/python/exmo3.py#L21的这一部分来看,随机数出现在请求URL中,而不是标题中:

def api_query(self, api_method, params = {}):
    params['nonce'] = int(round(time.time() * 1000))
    params =  urllib.parse.urlencode(params)

    sign = self.sha512(params)
    headers = {
        "Content-type": "application/x-www-form-urlencoded",
        "Key": self.API_KEY,
        "Sign": sign
    }
    conn = http.client.HTTPSConnection(self.API_URL)
    conn.request("POST", "/" + self.API_VERSION + "/" + api_method, params, headers)
    response = conn.getresponse().read()

答案 1 :(得分:0)

<?php
$key = 'K-5.........';
$secret = 'S-8..........';

$mt = explode(' ', microtime());
$NONCE = $mt[1] . substr($mt[0], 2, 6);

//https://api.exmo.com/v1.1/wallet_history
//https://api.exmo.com/v1.1/user_info
//https://api.exmo.com/v1.1/wallet_operations
//https://api.exmo.com/v1.1/deposit_address
//https://api.exmo.com/v1.1/withdraw_get_txid

$url = "https://api.exmo.com/v1.1/wallet_operations"; 

$req['nonce'] = $NONCE;

// generate the POST data string
$post_data = http_build_query($req, '', '&');

$sign = hash_hmac('sha512', $post_data, $secret);

$headers = array(
'Sign: ' . $sign,
'Key: ' . $key,
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$output = curl_exec($ch);

$output = json_decode($output, true);

echo '<pre>';
var_dump($output);
echo '</pre>';
?>