亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法使用 JWT 令牌連接到 Apple App Store API

無法使用 JWT 令牌連接到 Apple App Store API

catspeake 2023-10-20 15:12:52
我需要為 Store Connect API 生成 JWT 令牌。這是我的令牌生成代碼:console.log("?? appStoreConnectAPIFromNode.js running ???")const fs   = require('fs');const jwt  = require('jsonwebtoken'); // npm i jsonwebtoken// You get privateKey, apiKeyId and issuerId from your Apple App Store Connect accountconst privateKey = fs.readFileSync("./AuthKey-XXXXXXXX.p8") // this is the file you can only download once and should treat like a real, very precious key.const apiKeyId = "XXXXXXXX"const issuerId = "XXXXXXXX-XXXX-XXXX-XXX-XXXXXXXXXX"let now = Math.round((new Date()).getTime() / 1000); // Notice the /1000 let nowPlus20 = now + 1199 // 1200 === 20 minuteslet payload = {    "iss": issuerId,    "exp": nowPlus20,    "aud": "appstoreconnect-v1",    "iat": now}let signOptions = {    "algorithm": "ES256", // you must use this algorythm, not jsonwebtoken's default    header : {        "alg": "ES256",        "kid": apiKeyId,        "typ": "JWT"    }};let token = jwt.sign(payload, privateKey, signOptions);console.log('@token: ', token);fs.writeFile('Output.txt', token, (err) => {           // In case of a error throw err.     if (err) throw err; })我收到了這個回復 "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"        }]我認為問題在于令牌(直接帶有簽名)。當我嘗試在https://jwt.io/#debugger-io上解碼令牌時,我的有效負載和標頭已正確解碼。狀態:簽名無效我做錯了什么?有什么想法如何正確地做到這一點嗎?
查看完整描述

4 回答

?
絕地無雙

TA貢獻1946條經驗 獲得超4個贊

我也遇到這個問題,我和隊友解決了。。。。。


解決方案:


刪除有效負載中的“iat”(這很重要):像這樣,

let payload = {

     iss: *******,

     exp: *******,

     aud: "appstoreconnect-v1",

     bid: "your_bundle_id",

   };

網址:我用的是axios:

const { data } = await axios({

     method: "GET",

     url: "https://api.storekit.itunes.apple.com/inApps/v1/history/",

     headers: {

       "Content-type": "application/json",

       Authorization: `Bearer ${token}`,

     },

   });

res.json(data);


查看完整回答
反對 回復 2023-10-20
?
慕標5832272

TA貢獻1966條經驗 獲得超4個贊

我最近也需要解決這個問題,在學習了很多教程之后,這些教程經常給出應用程序商店連接 api 的錯誤答案。我找到了一種讓它發揮作用的方法。我在 Java 中就是這樣做的。


import com.auth0.jwt.JWT;

import com.auth0.jwt.algorithms.Algorithm;

import org.bouncycastle.openssl.PEMParser;

import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;

import java.security.spec.PKCS8EncodedKeySpec;

import java.security.interfaces.ECPrivateKey;

import java.security.KeyFactory;

import java.io.FileReader;

import java.util.Base64;

import java.time.Instant;

import java.time.temporal.ChronoUnit;


// The code is like this so try it in whatever method you want, 

// break it up into pieces so it's a bit more readable


// These variables are the minimum you should need

String keyFile = "/path/to/app_store_AuthKey_XXXX.p8";

// The XXXX is the same XXXX from the keyFile filename itself

String keyId = "XXXX";

String issuerId = "1234-1234-1234-1234-1234";


FileReader kfReader = new FileReader(keyFile);

PEMParser pemParser = new PEMParser(kfReader);

PrivateKeyInfo info = (PrivateKeyInfo) pemParser.readObject();

byte[] pkData = info.getEncoded();


// Convert the PrivateKeyInfo object to a PKCS8EncodedKeySpec object

PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pkData);


KeyFactory keyFactory = KeyFactory.getInstance("EC");

ECPrivateKey privateKey = (ECPrivateKey) keyFactory.generatePrivate(keySpec);


Algorithm algorithm = Algorithm.ECDSA256(null, privateKey);


Instant expiration = Instant.now().plus(TIMEOUT_MINUTES, ChronoUnit.MINUTES);


return JWT.create()

          .withKeyId(keyId)

          .withIssuer(issuerId)

          .withExpiresAt(expiration)

          .withAudience("appstoreconnect-v1")

          .sign(algorithm);


查看完整回答
反對 回復 2023-10-20
?
Helenr

TA貢獻1780條經驗 獲得超4個贊

我試圖解決我自己的問題,即使用 JWT 連接到 Apple Music API,我在您的有效負載中注意到了兩件事。首先,您將 1200 添加到 IAT 中,修復了我的問題,所以謝謝您。第二個是函數現在返回一個浮點數。我認為 API 需要一個 int。我會嘗試"iat": parseInt(now)或者now = Math.floor(new Date().getTime() / 1000)



查看完整回答
反對 回復 2023-10-20
?
呼啦一陣風

TA貢獻1802條經驗 獲得超6個贊

根據jsonwebtoken使用說明。options您可以直接在空負載上使用,如下所示:


let signOptions = {

? ? issuer: issuerId,

? ? keyid: apiKeyId,

? ? expiresIn: '20m',

? ? audience: 'appstoreconnect-v1',

? ? algorithm: 'ES256'

};


let token = jwt.sign({}, privateKey, signOptions);


查看完整回答
反對 回復 2023-10-20
  • 4 回答
  • 0 關注
  • 389 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號