簡體   English   中英

帶有 Curl 和 Php 的 Oauth 1.0 的 Creta 簽名 - 修復錯誤“標頭中不支持的簽名方法。需要 HMAC-SHA256”

[英]Creta Signature for Oauth 1.0 with Curl and Php - Fixing error "Unsupported signature method in the header. Require 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個錯誤:

  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 沒有雙引號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM