API Authentication

I am trying to migrate some of my mapkit code to the Map Server API. My JWT is fine and I can use that within the API Playground just fine. However, when I try to implement a test using PHP and cURL to get my Map token, I receive a 401 Not Authorized Error. My code is below:

<?php
$URL="https://maps-api.apple.com/v1/token";
$accesstoken = "<MY TOKEN HERE>";
$authorization = 'Authorization: Bearer'.$accesstoken;
$headers = [];

$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_HTTPHEADER, [$authorization]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADERFUNCTION,
  function($curl, $header) use (&$headers)
  {
    $len = strlen($header);
    $header = explode(':', $header, 2);
    if (count($header) < 2) // ignore invalid headers
      return $len;

    $headers[strtolower(trim($header[0]))][] = trim($header[1]);
    
    return $len;
  }
);
$result=curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   //get status code

curl_close ($ch);

print_r($headers);
echo($result);
echo($status_code);
?>

Now the exact response that I am receiving is:

Array ( [date] => Array ( [0] => Thu, 18 Apr 2024 18:32:15 GMT ) [content-type] => Array ( [0] => application/json;charset=utf8 ) [content-length] => Array ( [0] => 51 ) [connection] => Array ( [0] => keep-alive ) [cache-control] => Array ( [0] => max-age=0 ) [x-rid] => Array ( [0] => c9507281-bc32-46ac-be3b-dc59e97e7fed ) [strict-transport-security] => Array ( [0] => max-age=31536000; includeSubDomains; ) ) 

{"error":{"message":"Not Authorized","details":[]}} 
401

Any help getting this to work would be appreciated.

  • After stepping away for a bit, I solved my own issue. I was missing a space between Bearer and my access token. Doh!

    $authorization = 'Authorization: Bearer '.$accesstoken;
    
Add a Comment