AppStore 连接 API 401 错误,同时使用 Firebase\JWT
AppStore connect API 401 ERROR, while using Firebase\JWT
我正在尝试从 AppleStore connect API 获取报告和数据,但它一直给我 401 错误。
{ "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" }]
}
我使用此处的指南 => generating_tokens_for_api_requests 并使用 firebase/php-jwt
库为 Auth 创建 jwt 令牌。在 App store 连接中,密钥具有 Developer
访问权限。
我的示例代码如下:
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;
}
AuthKey_XXXXXXXXX.p8
文件内容如下:
-----BEGIN PRIVATE KEY-----
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
-----END PRIVATE KEY-----
我为这个问题苦苦挣扎了两天,如果有人能解决这个问题或给出一些解决这个问题的提示,将不胜感激。
guzzle
有问题。当我在没有异步的情况下使用它时。它开始工作
$response = $client->request('GET', 'apps', ['headers' => ['Authorization' => "Bearer $jwt"]])->getBody()->getContents();
如果有人能给我一个理由,为什么当我使用异步时会发生这种情况,学习它会很有趣。
我正在尝试从 AppleStore connect API 获取报告和数据,但它一直给我 401 错误。
{ "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" }] }
我使用此处的指南 => generating_tokens_for_api_requests 并使用 firebase/php-jwt
库为 Auth 创建 jwt 令牌。在 App store 连接中,密钥具有 Developer
访问权限。
我的示例代码如下:
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;
}
AuthKey_XXXXXXXXX.p8
文件内容如下:
-----BEGIN PRIVATE KEY-----
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
-----END PRIVATE KEY-----
我为这个问题苦苦挣扎了两天,如果有人能解决这个问题或给出一些解决这个问题的提示,将不胜感激。
guzzle
有问题。当我在没有异步的情况下使用它时。它开始工作
$response = $client->request('GET', 'apps', ['headers' => ['Authorization' => "Bearer $jwt"]])->getBody()->getContents();
如果有人能给我一个理由,为什么当我使用异步时会发生这种情况,学习它会很有趣。