api - Creta Signature for Oauth 1.0 with Curl and Php - Fixing error "Unsupported signature method in the header. Require HMAC-SHA256" ← (PHP, HTML)

What is wrong?

This is all my code, I think that it should be fine, but I don't know why... with postman I haven't errors and I receive the tokens while with Curl I receive this error. I'm working in local with 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;
  • Result basestring (dump on line 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"

And on the doc here we have this example... it look the same:

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

Answer



Solution:

FIXED

Finally... after 2 days... There are 2 errors on the code above:

  1. The parameter $data to CURLOPT_POSTFIELDS
  2. Oauth 1.0 wants the double quotes.

So change:

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

With

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

This is strange for me, because with curl I use http_build_query with GET request and not POST, where we can use $data directly like array.
And delete:

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

And write the query with your beautifuls hand because $params doesn't have double quote.

Source