【问题标题】:php jwt token authentication failed to apple store connect apiphp jwt 令牌身份验证未能通过苹果商店连接 api
【发布时间】:2020-06-05 08:18:56
【问题描述】:

根据以下论坛链接参考:

https://forums.developer.apple.com/thread/119705, https://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), '+/', '-_'), '=');

}

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

【问题讨论】:

    标签: php jwt


    【解决方案1】:

    您使用“sha256”算法进行签名,但 App Store Connect API 密钥是 ES256 密钥。更改您的签名算法,或者为了简化事情,使用 JWT 库。您可以在jwt.io 找到几个 php 选项。一定要选择支持 ES256 的。

    【讨论】:

      猜你喜欢
      • 2019-08-23
      • 2020-08-23
      • 1970-01-01
      • 2018-01-20
      • 1970-01-01
      • 1970-01-01
      • 2017-10-22
      • 1970-01-01
      • 2019-10-14
      相关资源
      最近更新 更多