简体   繁体   English

JWT 生成的令牌在提交到 Apple 服务器时始终导致 401

[英]JWT Token generated always results in 401 when submitted to Apple servers

I am using the following code in PHP to generate a JWT token to use with the App Store Server API:我在 PHP 中使用以下代码生成一个 JWT 令牌以与 App Store 服务器 API 一起使用:

$keyId = 'provided by apple';
    $priv_key = <<<EOD
    -----BEGIN PRIVATE KEY-----
    provided by apple
    -----END PRIVATE KEY-----
    EOD;

    $pem_private_key = file_get_contents('AuthKey.pem');

    $private_key = openssl_pkey_get_private($pem_private_key);
    $public_key_pem = openssl_pkey_get_details($private_key)['key'];
    $public_key = openssl_pkey_get_public($public_key_pem);
   
    $headers = [
        "kid"=> $keyId,
        "typ" => 'JWT'
    ];
    $payload = [
        "iss" => "provided by apple",
        "iat"  => time(),
        "exp"  => time() + (60 * 60 * 24 * 7),
        "aud" => "appstoreconnect-v1",
        "bid" => "com.bundle.my"
    ];
    $jwt = JWT::encode($payload, $priv_key, 'ES256', $keyId, $headers); 

This results in a JWT token.这会产生 JWT 令牌。 However when I try to use it with the Apple Store API, I always get 401 Unauthorized;但是,当我尝试将它与 Apple Store API 一起使用时,我总是得到 401 Unauthorized; Unauthenticated.未经验证。

What am I doing wrong?我究竟做错了什么?

The problem was the expiration time as Apple only allows an expiration of max 20 mins.问题是到期时间,因为 Apple 只允许最多 20 分钟的到期时间。

Changed to time() + 1200.更改为时间 () + 1200。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用OpenID Connect ID令牌时,Passport JWT总是返回未经授权的401 - Passport JWT is always returning 401 unauthorized when using OpenID Connect ID Token Jwt 令牌总是返回 401 .net core 3.1 web api - Jwt token always return 401 .net core 3.1 web api 401 JWT 找不到令牌 - 401 JWT Token not found 验证后 JWT 令牌始终无效 - JWT token is always invalid when verified Apple Store Connect 的 Python JSON Web 令牌 (JWT) GET 请求 401 错误 - Python JSON Web token (JWT) GET request 401 error with Apple Store Connect 401 无效的 JWT 令牌 Lexik - 401 Invalid JWT Token Lexik Laravel JWT 使用自定义声明生成的身份验证令牌获得 401 unauthenticate - Laravel JWT auth token generated using custom claims getting 401 unauthenicate 为 Apple Maps 创建 jwt 令牌时有多个来源? - Multiple origin when creating jwt token for Apple Maps? 将 JWT 与 Laravel 和 Axios 一起使用时,JWT 的其他路线总是返回4 - When using JWT with Laravel and Axios, JWT works but other routes always returns 401 Unauthenticated 具有 JWT 身份验证的 MERN 应用程序在 cookie 上存储令牌总是在请求时返回 401 - MERN App with JWT authentication storing token on cookie always returns 401 on requests
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM