我试图了解如何使用 TradeKing API 发布数据,该 API 使用 OAuth 库。在与 API 支持专家交谈后,他解释说唯一缺少的是完成请求所需的 OAuth header 。
我对 OAuth 的经验非常有限,并且官方文档中没有一个 POST
PHP 示例。我感谢任何有关如何成功生成适合 API 的 OAuth header 的帮助。
我尝试在下面的代码中使用以下 URL 中的示例,但是,它似乎已被贬值,并且示例中使用的所有内置函数在最新版本中都丢失了:
https://gist.github.com/JCBarry/2853042
提前致谢!
官方POST
文档
https://developers.tradeking.com/documentation/accounts-id-orders-post
这是我尝试过的:
<?php
// Your keys/secrets for access
$consumer_key = '0cc175b9c0f1b6a831c399e269772661';
$consumer_secret = 'ff2513194e75315625628304c9eb66e8';
$access_token = '150a96573adf12b21dab621e85497e6e';
$access_secret = '5c7b57d450a71d378a5eda991f809e56';
$oauth = new OAuth($consumer_key,$consumer_secret,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_AUTHORIZATION);
$oauth->disableSSLChecks();
$oauth->setToken($access_token,$access_secret);
$url = 'https://api.tradeking.com/v1/accounts/12345678/orders/preview.xml';
$data = '<?xml version="1.0"?>
<FIXML xmlns="http://www.fixprotocol.org/FIXML-5-0-SP2">
<Order TmInForce="0" Typ="1" Side="1" Acct="12345678">
<Instrmt SecTyp="CS" Sym="F"/>
<OrdQty Qty="1"/>
</Order>
</FIXML>';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
echo $response = curl_exec($ch);
?>
这是我收到的错误:
错误 AuthenticationFailuresignature_method_rejected/v1/accounts/12345678/orders/preview.xml
最佳答案
根据 API,您需要发送如下所示的授权 header
/*
Authorization: OAuth
oauth_consumer_key="672e71387c0a126f4e13296ecfb2b7bc",
oauth_nonce="cfdb7f42de425d94fbc24c11a920c33e",
oauth_signature="Vfm148kAgBZ6azEHqds43aY2UsEg3D",
oauth_signature_method="HMAC-SHA1",
oauth_timestamp="1306871932",
oauth_token="819682555b63ab8bd90a4138138e7365",
oauth_version="1.0"
*/
尝试下面的代码。您需要定义 $oauth_nonce
和 $oauth_signature
。
$header = [
"Authorization: OAuth \n" .
'oauth_consumer_key="'.$consumer_key.'"' . " \n" .
'oauth_nonce="'.$oauth_nonce.'"' . " \n" .
'oauth_signature="'.$oauth_signature.'"' . " \n" .
'oauth_signature_method="HMAC-SHA1"' . " \n" .
'oauth_timestamp="'.time().'"' . " \n" .
'oauth_token="'.$access_token.'"' . " \n" .
'oauth_version="1.0"'
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
关于php - 生成基本 OAuth header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24101856/