php jwt token authentication failed to apple store connect api

Hi,

As per below forum link reference :

https://forums.developer.apple.com/thread/119705

https://forums.developer.apple.com/thread/117754


I try to connect appstoreconnectapi using PHP but getting below error each time :


stdClass Object

(

[errors] => Array

(

[0] => stdClass Object

(

[status] => 401

[code] => NOT_AUTHORIZED

[title] => Authentication credentials are missing or invalid.

[detail] => Provide a properly configured and signed bearer token, and make sure that it has not expired. Learn more about Generating Tokens for API Requests https://developer.apple.com/go/?id=api-generating-tokens

)

)

)

Please check my below code once and my php xammp version 7.4 :


date_default_timezone_set("Asia/Kolkata");

$strKey=file_get_contents("./AuthKey_##########.p8");

$strUrl = "https://api.appstoreconnect.apple.com/v1/users";

$dHeader = array("kid" => "##########", "typ" => "JWT");

$strHeader = json_encode($dHeader);

$strBase64Header = base64url_encode($strHeader);

$dPayLoad = array("iss" => "69a#####-####-####-####-############", "exp"=>time()+1200, "aud"=>"appstoreconnect-v1");

$strPayLoad = json_encode($dPayLoad);

$strBase64PayLoad = base64url_encode($strPayLoad);

$strContent = $strBase64Header . '.' . $strBase64PayLoad;

$strAlgName = "sha256";

$strSignature = base64url_encode(hash_hmac($strAlgName, $strContent, $strKey));

$strToken = $strBase64Header . '.' . $strBase64PayLoad . '.' . $strSignature;

$curl = curl_init();

curl_setopt_array($curl, array(

CURLOPT_URL => "https://api.appstoreconnect.apple.com/v1/users",

CURLOPT_RETURNTRANSFER => true,

CURLOPT_ENCODING => "",

CURLOPT_MAXREDIRS => 10,

CURLOPT_TIMEOUT => 30,

CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,

CURLOPT_CUSTOMREQUEST => "GET",

CURLOPT_HTTPHEADER => array(

"Authorization: Bearer ".$strToken

),

));


$response = curl_exec($curl);

$err = curl_error($curl);

curl_close($curl);

echo "<pre>";

if ($err) {

echo "cURL Error #:" . $err."<br>";

print_r(json_decode($response));

} else {

print_r(json_decode($response));

}



function base64url_encode($data) {

return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');

}


Please provide details code if I did mistake in above .

Replies

A little solution https://github.com/animus-coop/apple-laravel-authorization-code for laravel