php - 带有 Curl 和 Php 的 Oauth 1.0 的 Creta 签名 - 修复错误“标头中不支持的签名方法。需要 HMAC-SHA256”
问题描述
怎么了?
这是我所有的代码,我认为应该没问题,但我不知道为什么......使用邮递员我没有错误,我收到了令牌,而使用 Curl 我收到了这个错误。我在本地与 Mamp pro 一起工作。
$url = "https://account.api.here.com/oauth2/token";
$data = array(
"grant_type" => "client_credentials"
);
$oauthNonce = mt_rand();
$oauthTimestamp = time();
$oauthCustomereKey = "******";
$oauthSignatureMethod = "HMAC-SHA256";
$httpMethod = "POST";
$oauthVersion = "1.0";
$keySecret = "*****";
$baseString = $httpMethod."&". urlencode($url);
$paramString =
"grant_type=client_credentials&".
"oauth_consumer_key=". urlencode($oauthCustomereKey).
"&oauth_nonce=". urlencode($oauthNonce).
"&oauth_signature_method=". urlencode($oauthSignatureMethod).
"&oauth_timestamp=". urlencode($oauthTimestamp).
"&oauth_version=". urlencode($oauthVersion)
;
$baseString = $baseString . "&" . urlencode($paramString);
var_dump($baseString);
$signingKey= urlencode($keySecret) . "&";
$signature = urlencode(
base64_encode(
hash_hmac(
'sha256',
$baseString,
$signingKey,
true
)
)
);
$params = [
"oauth_consumer_key" => $oauthCustomereKey,
"oauth_nonce" => $oauthNonce,
"oauth_signature_method" => $oauthSignatureMethod,
"oauth_timestamp" => $oauthTimestamp,
"oauth_version" => $oauthVersion,
"oauth_signature" => $signature
];
// $lol = 'oauth_consumer_key="' . $oauthCustomereKey . '",oauth_signature_method="'.$oauthSignatureMethod . '",oauth_timestamp="'.$oauthTimestamp.'",oauth_nonce="'.$oauthNonce.'",oauth_version="'.$oauthVersion.'",oauth_signature="'.$signature.'"';
/* This will give you the proper encoded string to include in your Authorization header */
$params = http_build_query($params, null, ',', PHP_QUERY_RFC3986);
$authorization = "Authorization: OAuth " . $params;
var_dump($authorization);
if(!$curl = curl_init()){
exit;
}
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
"Content-Type : application/x-www-form-urlencoded",
$authorization));
$token = curl_exec($curl);
curl_close($curl);
return $token;
- 结果基串(第 87 行转储):
string(253) "POST&https%3A%2F%2Faccount.api.here.com%2Foauth2%2Ftoken&grant_type%3Dclient_credentials%26oauth_consumer_key%3D********%26oauth_nonce%3D1468397107%26oauth_signature_method%3DHMAC-SHA256%26oauth_timestamp%3D1605523226%26oauth_version%3D1.0"
在这里的文档中,我们有这个例子......它看起来一样:
POST
&https%3A%2F%2Faccount.api.here.com%2Foauth2%2Ftoken
&grant_type=client_credentials%26oauth_consumer_key%3Daccess-key-id-1234%26oauth_nonce%3DLIIpk4%26
oauth_signature_method%3DHMAC-SHA256%26oauth_timestamp%3D1456945283%26oauth_version%3D1.0
解决方案
固定的
终于...... 2天后......上面的代码有2个错误:
- CURLOPT_POSTFIELDS 的参数 $data
- Oauth 1.0 需要双引号。
所以改变:
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
和
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
这对我来说很奇怪,因为在 curl 中我使用带有 GET 请求而不是 POST 的 http_build_query,我们可以像数组一样直接使用 $data。
并删除:
$params = http_build_query($params, null, ',', PHP_QUERY_RFC3986);
并用你美丽的手编写查询,因为 $params 没有双引号。
推荐阅读
- pandas - 如何处理熊猫中的同名列
- prolog - Prolog:为什么这个谓词没有失败,一个空列表作为参数传递
- python - Gensim LDA:如何获取每个主题中单词的数量(频率)?
- git - git hook 批量取消暂存路径中的所有文件
- mysql - MySQL:如何为 original_id 添加一列?
- c++ - NtQueryInformationProcess 返回 STATUS_ACCESS_VIOLATION
- html - 如果每个项目都有多个需要水平对齐的列,您将如何设置树视图的样式?(网络)
- amazon-web-services - 雅典娜分区限制以及如何最好地解决我所描述的问题
- javascript - 尝试使用 node.js 在 readme.md 中放置许可证徽章但未定义错误参数
- python - 如何在python中正确实现生产者消费者