PHP JWT 令牌身份验证失败到 Apple Store Connect API



根据以下论坛链接参考:

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

我尝试使用 PHP 连接应用商店连接(https://developer.apple.com/documentation/appstoreconnectapi(,但每次都出现以下错误:

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

请检查我的以下代码一次和我的 php xammp 版本 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), '+/', '-_'), '=');
}

如果我在上面做错了,请提供详细的代码。

您使用"sha256"算法进行签名,但App Store Connect API密钥是ES256密钥。更改签名算法,或者为了简化操作,请使用 JWT 库。您可以在 jwt.io 找到几个 php 选项。请务必选择支持ES256的产品。

最新更新