I am trying to get reports and data from AppleStore connect API, but it keeps giving me 401 ERROR.
My sample code for it as follows:
I am struggling with this issue for two days, if anyone can fix or give some hints to fix this issue, will be much appreciated.
I was using the guidelines from generating_tokens_for_api_requests, guidelines and using firebase/php-jwt library for PHP to create jwt token for the Auth. I am using guzzle for sending the API request. In the App store the key has Developer access privileges.{ "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 }] }
My sample code for it as follows:
Code Block date_default_timezone_set("Europe/Madrid"); use \Firebase\JWT\JWT; $appleCon = [ 'privateKey' => "" . file_get_contents(DIR . '/AuthKey_XXXXXXXXX.p8'), 'apiKeyID' => 'XXXXXXXXX', 'issuerID' => 'XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX' ]; $payload = array( "iss" => $appleCon['issuerID'], "exp" => time() + 1200, "aud" => "appstoreconnect-v1", ); $jwt = JWT::encode($payload, $appleCon['privateKey'], "ES256", $appleCon['apiKeyID']); $client = new GuzzleHttp\Client(['base_uri' => 'https://api.appstoreconnect.apple.com/v1/']); $request = new \GuzzleHttp\Psr7\Request('GET', 'apps', [ 'headers' => [ 'Authorization' => "Bearer ".$jwt ] ]); $promise = $client->sendAsync($request)->then(function ($response) { echo 'I completed! ' . $response->getBody(); }); try { $promise->wait(); } catch (GuzzleHttp\Exception\ClientException $e) { $response = $e->getResponse(); $responseBodyAsString = $response->getBody()->getContents(); echo $responseBodyAsString; }
I am struggling with this issue for two days, if anyone can fix or give some hints to fix this issue, will be much appreciated.