问题描述
您好,有人可以通过Cardinity API来帮助我。
https://developers.cardinity.com/api/v1/?shell#authentication
我想使用Curl PHP进行身份验证,但是无法正常工作。我收到来自基数的错误消息。
{“类型”:“ https://developers.cardinity.com/api/v1/#401”,“标题”:“未经授权”,“状态”:401,“详细信息”:“ OAuth使用者凭据不足。” }
这是我的代码。
<?PHP
$consumer_key = "asd";
$consumer_sec = "adefg";
$url = 'https://api.cardinity.com/v1/payments';
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$timestamp = time();
$nonce = md5($timestamp);
//Generate Signature
$base = 'POST&'.$url.'&oauth_consumer_key='.$consumer_key.'&oauth_signature_method=HMAC-SHA1&oauth_timestamp='.$timestamp.'&oauth_nonce='.$nonce.'&oauth_version=1.0&oauth_token=';
$base = rawurlencode($base);
$signature = base64_encode(hash_hmac('sha1',$base,rawurlencode($consumer_sec),true));
//
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: OAuth oauth_consumer_key="'.$consumer_key.'",oauth_signature_method="HMAC-SHA1",oauth_timestamp="'.$timestamp.'",oauth_nonce="'.$nonce.'",oauth_version="1.0",oauth_token="",oauth_signature="'.$signature.'"';
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
echo $result;
?>
解决方法
您的OAuth签名未正确生成。您的OAuth复合密钥错误。这是HMACSha1的文档 https://tools.ietf.org/html/rfc5849#section-3.4.2
要点:假设您要使用的复合密钥需要同时具有客户端共享密钥和令牌共享密钥。中间带有“&”。
的
db.system.js.save({_id:"testValue",date: { $dateToString: { date: new Date(),format: "%Y-%m-%d"} } })
如果令牌为空,则需要使用hash_hmac的第三个参数
%Vector relating to the sample/data point number
Sample = linspace(1,70,70);
%Random test data
X = randi([0,2],1,70);
stem(Sample,X);
%Key indices to change color
Key_Indices = [7 10 11 12 15 18 48 50 52 60];
line(Sample(Key_Indices),X(Key_Indices),'linestyle','none','marker','o','color','r')
axis([0 70 0 3])
grid on
这里是通过curl使用cardinity api的完整示例
rawurlencode($consumer_sec)