使用 Curl 和 PHP 创建 Oauth 1.0 签名 - 修复错误“标头中不支持的签名方法。需要 HMAC-SHA256”

问题描述 投票:0回答:1

出了什么问题?

这是我的全部代码,我认为应该没问题,但我不知道为什么......使用 Postman 时我没有错误,并且收到了令牌,而使用 Curl 时我收到了此错误。我在本地与 Mamp pro 一起工作。

  • 收到的错误是:

  • 我的代码生成Signature并获取Token

$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
php curl oauth
1个回答
0
投票

已修复

终于...两天后... 上面的代码有两个错误:

  1. 参数$data转为CURLOPT_POSTFIELDS
  2. Oauth 1.0 需要双引号。

所以改变:

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));

这对我来说很奇怪,因为在curl中我使用http_build_query和GET请求而不是POST,我们可以像数组一样直接使用$data。
并删除:

$params = http_build_query($params, null, ',', PHP_QUERY_RFC3986);

用你漂亮的手写查询,因为 $params 没有双引号。

© www.soinside.com 2019 - 2024. All rights reserved.