Problems in access to Google API OAuth2 Service Account with google-api-php-client. What I surprised to discover is this part of code from example of a JWT claim set in Google Dev returning wrong JWT Claim set when for some reason JWT Header pass true what making me feel like UTF-8 serialization with json_encode() creating an error here.
$JWT_Header = array(
"alg" => "RS256",
"typ" => "JWT"
);
$JWT_Body = array(
"iss" => "761326798069-r5mljlln1rd4lrbhg75efgigp36m78j5@developer.gserviceaccount.com",
"scope" => "https://www.googleapis.com/auth/prediction",
"aud" => "https://accounts.google.com/o/oauth2/token",
"exp" => 1328554385,
"iat" => 1328550785
);
$segments = array(
rtrim(strtr(base64_encode(json_encode($JWT_Header)), '+/', '-_'), '='),
rtrim(strtr(base64_encode(json_encode($JWT_Body)), '+/', '-_'), '=')
);
//echo '<pre>'; print_r($segments); echo '</pre>';
different from what is described in Google Dev: Encoding the JWT Claim Set example of output
/*
Output:
JWT header:
Google Dev eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.
My output eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.
JWT Claim set:
Google Dev eyJpc3MiOiI3NjEzMjY3OTgwNjktcjVtbGpsbG4xcmQ0bHJiaGc3NWVmZ2lncDM2bTc4ajVAZGV2ZWxvcGVyLmdzZXJ2aWNlYWNjb3VudC5jb20iLCJzY29wZSI6Imh0dHBzOi8vd3d3Lmdvb2dsZWFwaXMuY29tL2F1dGgvcHJlZGljdGlvbiIsImF1ZCI6Imh0dHBzOi8vYWNjb3VudHMuZ29vZ2xlLmNvbS9vL29hdXRoMi90b2tlbiIsImV4cCI6MTMyODU1NDM4NSwiaWF0IjoxMzI4NTUwNzg1fQ.
My output eyJpc3MiOiI3NjEzMjY3OTgwNjktcjVtbGpsbG4xcmQ0bHJiaGc3NWVmZ2lncDM2bTc4ajVAZGV2ZWxvcGVyLmdzZXJ2aWNlYWNjb3VudC5jb20iLCJzY29wZSI6Imh0dHBzOlwvXC93d3cuZ29vZ2xlYXBpcy5jb21cL2F1dGhcL3ByZWRpY3Rpb24iLCJhdWQiOiJodHRwczpcL1wvYWNjb3VudHMuZ29vZ2xlLmNvbVwvb1wvb2F1dGgyXC90b2tlbiIsImV4cCI6MTMyODU1NDM4NSwiaWF0IjoxMzI4NTUwNzg1fQ.
*/
Any suggestions?
It doesn’t seem to be related to UTF-8 in any way. To debug, just apply the same algorithm backwards:
You’ll see that Google JSON string contains the same values, except that they don’t escape forward slashes: