php jwt令牌身份验证未能通过Apple存储连接api

时间:2020-02-07 10:48:10

标签: php jwt

按照下面的论坛链接参考:

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

我尝试使用PHP连接 app-store-connect 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), '+/', '-_'), '=');

}

如果我在上面没有记错,请提供详细代码。

1 个答案:

答案 0 :(得分:0)

您正在使用“ sha256”算法进行签名,但是App Store Connect API密钥是ES256密钥。更改您的签名算法,或者为简化起见,请使用JWT库。您可以在jwt.io找到几个php选项。确保选择一种支持ES256的软件。