首页 > 解决方案 > 带有 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;
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

标签: phpapicurloauth

解决方案


固定的

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

  1. CURLOPT_POSTFIELDS 的参数 $data
  2. 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 没有双引号。


推荐阅读