How to make oauth2 for here map API in php

How to make oauth2 for here map API in php

How to make oauth2 for here map API in php

To make oauth2 authentication in PHP for here map you need bellow credentials:-

1) here.access.key.id

2) here.access.key.secret

Now use below code snippet to create an access token

$timer = (string) time();
$grant_type = 'client_credentials';
$oauth_consumer_key = 'here.access.key.id';
$oauth_nonce = uniqid(mt_rand(1, 1000));
$oauth_signature_method = 'HMAC-SHA256';
$oauth_timestamp = $timer;
$oauth_version = '1.0';
$url = 'https://account.api.here.com/oauth2/token';
$access_key_secret = "here.access.key.secret";

$parameter_string = 'grant_type='.$grant_type;
$parameter_string .= '&oauth_consumer_key='.$oauth_consumer_key;
$parameter_string .= '&oauth_nonce='.$oauth_nonce;
$parameter_string .= '&oauth_signature_method='.$oauth_signature_method;
$parameter_string .= '&oauth_timestamp='.$oauth_timestamp;
$parameter_string .= '&oauth_version='.$oauth_version;
$encoded_parameter_string = urlencode($parameter_string);

$encoded_base_string = 'POST'.'&'.urlencode($url).'&'.$encoded_parameter_string;

$signing_key = $access_key_secret.'&';

$signature = hash_hmac('SHA256', $encoded_base_string, $signing_key, true);

$encodedSignature = urlencode(base64_encode($signature));

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://account.api.here.com/oauth2/token',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => 'grant_type=client_credentials',
  CURLOPT_HTTPHEADER => array(
    'Authorization: OAuth oauth_consumer_key="'.$oauth_consumer_key.'",oauth_signature_method="HMAC-SHA256",oauth_timestamp="'.$oauth_timestamp.'",oauth_nonce="'.$oauth_nonce.'",oauth_version="1.0",oauth_signature="'.$encodedSignature.'"',
    'Content-Type: application/x-www-form-urlencoded'
  ),
));

$response = curl_exec($curl);
curl_close($curl);

echo $response;

Now you have a bearer access token using the above code. Use this token to use here map API’s

Scroll to Top