"status": "401", "code": "NOT_AUTHORIZED", "title": "Authentication credentials are missing or invalid."

I do the work "Generating Tokens for API Requests".

Return this error:

"errors": [{

"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"

}]


//-----------------------------------------------------------------------------------

My php Code is :


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

$dHeader = array("alg" => "ES256", "kid" => "XXXX5XWNA4", "typ" => "JWT");

$strHeader = json_encode($dHeader);

$strBase64Header = Base64UrlEncode($strHeader, JSON_UNESCAPED_UNICODE);


$dPayLoad = array("iss" => "4f830cec-8495-4a09-abae-52030dda7fbb", "exp"=>time()+1200, "aud"=>"appstoreconnect-v1");

$strPayLoad = json_encode($dPayLoad);

$strBase64PayLoad = Base64UrlEncode($strPayLoad, JSON_UNESCAPED_UNICODE);


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


$strAlgName = "sha256";

$strSignature = Base64UrlEncode(hash_hmac($strAlgName, $strContent, $strKey, true));

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

//-----------------------------------------------------------------------------------


So, I get the strToken. The token can be decoded to right data(header,payload) In this web page("https://jwt.io)


But, I get error when I excute the command:


curl -v -H 'Authorization: Bearer xxxxxx my token xxxxxxxxxxxxxxxxxxxxxxxxxxx' "https://api.appstoreconnect.apple.com/v1/salesReports"


Return this error:

"errors": [{

"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"

}]

//-----------------------------------------------------------------------------------


Why? Help me ,please. I already spend 2 days for this problem.

Replies

that answer do not help!
not sure if it helps you but i was getting this error with my python script using jwt and i had to decode the token to get rid of a b'' that was surrounding my token for my api connection to work.