Flutter jsonwebtoken 生成 Appstore Connect API 令牌无效

问题描述 投票:0回答:1

我正在尝试生成 Appstore Connect API 令牌来管理我的应用程序。我使用的软件包是

dart_jsonwebtoken
。代码:

final jwt = JWT({
    "iss": "YOUR_ISSUER_ID",
    "iat": DateTime.now().millisecondsSinceEpoch / 1000,
    "exp": DateTime.now().add(Duration(days: 30)).millisecondsSinceEpoch / 1000,
    "aud": "appstoreconnect-v1",
    "scope": [
      "GET /v1/apps?filter[platform]=IOS"
    ]
  }, header: {
    "alg": "ES256",
    "kid": "YOUR PRIVATE KEY ID",
    "typ": "JWT"
  });

  // Here I use the content from AuthKey_KEYID.p8 file
  final token = jwt.sign(ECPrivateKey('''-----BEGIN PRIVATE KEY-----
  YOUR AuthKey Content
  -----END PRIVATE KEY-----'''), algorithm: JWTAlgorithm.ES256);

  print('Signed token: $token\n');

令牌已成功生成,我正在使用它来调用 API,但收到无效令牌错误:

{
    "errors": [
        {
            "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"
        }
    ]
}

我很确定我的生成令牌的代码存在一些问题。有人可以帮我吗?预先感谢。

我使用下面的 ruby 代码让它工作。生成的令牌工作正常。我正在尝试使用 flutter 重写它。

require "base64"
require "jwt"
ISSUER_ID = "2ece1127-df4d-426a-a3eb-061e68f5eebe"
KEY_ID = "W9M2AM584Y"
private_key = OpenSSL::PKey.read(File.read('AuthKey_W9M2AM584Y.p8'))
token = JWT.encode(
   {
    iss: ISSUER_ID,
    exp: Time.now.to_i + 20 * 60,
    aud: "appstoreconnect-v1"
   },
   private_key,
   "ES256",
   header_fields={
     kid: KEY_ID }
 )
puts token
flutter dart jwt app-store-connect
1个回答
0
投票

我也在寻找同样的! 我尝试了你的 dart 代码,它对我有用。 对于 ISSUERID,我使用了我的 TEAMID。

© www.soinside.com 2019 - 2024. All rights reserved.