需要连接 iTunes Connect API,总是显示 not authorized for JWT

Need to connect iTunes Connect API, always shows not authorized for JWT

I'm trying to generate a token to sign my requests to the iTunes Connect API. That's my PHP file:

function encode($data)
{
    return str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($data));
}

function sign($data)
{
    if (!$key = openssl_pkey_get_private('file://AuthKey_qwerty.p8')) {
        throw new \Exception('Failed to read PEM');
    }

    if (!openssl_sign($data, $signature, $key, OPENSSL_ALGO_SHA256)) {
        throw new \Exception('Claims signing failed');
    }

    return $signature;
}

function create()
{

    $header = encode(
        json_encode([
            'kid' => 'frfc343r4',
            'alg' => 'ES256',
            'typ' => 'JWT',
        ])
    );

    $claims = encode(
        json_encode([
            'iss' => 'ddd-aaa-bbbb-cccc-ddddd',
            'exp' => time() + (20 * 60),
            'aud' => 'appstoreconnect-v1',
        ])
    );

    $signature = encode(
        sign("$header.$claims")
    );

    return $header . '.' . $claims . '.' . $signature;
}
echo create();

which gives an error, "Authentication credentials are missing or invalid.",App Store Connect API must be signed with ES256 encryption

require_once '../vendor/autoload.php';

    use Curl\Curl;
    use Lcobucci\JWT\Builder;
    use Lcobucci\JWT\Signer\Key;
    use Lcobucci\JWT\Signer\Ecdsa\Sha256;

    $signer = new Sha256();
    $privateKey = new Key('file://AuthKey_ed2erd424.p8');
    $time = time();

    $Issuer_ID = "3455355-3535-4f8g-8x2r-3dcfrr43ed33";
    $Key_ID = "4DD3R45DT45";

    $token = (new Builder())->issuedBy($Issuer_ID)// Configures the issuer (iss claim)
    ->permittedFor("appstoreconnect-v1")// Configures the audience (aud claim)
    //->identifiedBy('XXYYZZ', true)// Configures the id (jti claim), replicating as a header item
    ->withHeader('kid', $Key_ID)
    ->withHeader('type', 'JWT')
        ->withHeader('alg', 'ES256')
        ->issuedAt($time)// Configures the time that the token was issue (iat claim)
        ->expiresAt($time + 1200)// Configures the expiration time of the token (exp claim)
        ->withClaim('uid', 1)// Configures a new claim, called "uid"
        ->getToken($signer, $privateKey); // Retrieves the generated token


    $token->getHeaders(); // Retrieves the token headers
    $token->getClaims(); // Retrieves the token claims

    $date = $_GET['date'];
    $url ='https://api.appstoreconnect.apple.com/v1/salesReports';
    $dataArray = array(
                        'filter[frequency]'=>'DAILY',
                        'filter[reportDate]'=>$date,
                        'filter[reportSubType]'=>'SUMMARY',
                        'filter[reportType]'=>'SALES',
                        'filter[vendorNumber]'=>'345434463',
                        'filter[version]'=>'1_0'
                        );         

    $ch = curl_init();
    $data = http_build_query($dataArray);
    $getUrl = $url."?".$data;
    $authorization = "Authorization: Bearer ".$token; // Prepare the authorisation token
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization )); // Inject the token into the header
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_URL, $getUrl);
    curl_setopt($ch, CURLOPT_TIMEOUT, 80);

    $response = curl_exec($ch);

    if(curl_error($ch)){
        echo 'Request Error:' . curl_error($ch);
    }
    else
    {
      //  if(!gzdecode($response)){ echo $response; exit; }

      $uncompressed = $fp = @gzdecode($response);
        if ($uncompressed === false) {
        // do something related to error here
        echo $response; exit;
    }


        ;
        $fp1 = array();
        $myArray = [];
        $lines = explode(PHP_EOL, $uncompressed);
        $l = 0;
        foreach($lines as $line) {
            if($line == ''){continue;}
            if($l == 0){ $myArray[$l] = explode("\t", $line); $l++; continue;}
            //explode("\t", $line);
            $key =0;
            foreach(explode("\t", $line) as $value){
               $myArray[$l][$myArray[0][$key]] = $value;
               $key++;
            }
            $fp1[] = $myArray[$l];
            $l++;
        }
        echo json_encode($fp1);

        //echo $response;
    }

    curl_close($ch);