我需要为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 account
const 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 minutes
let 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"
}]
我认为问题出在token(直接使用签名)。当我在https://jwt.io/#debugger-io上尝试解码令牌时,我的有效负载和报头被正确解码。状态:签名无效
我做错了什么?你知道该怎么做吗?
发布于 2020-12-08 20:38:06
根据jsonwebtoken usage指令。您可以直接在空负载上使用options
,如下所示:
let signOptions = {
issuer: issuerId,
keyid: apiKeyId,
expiresIn: '20m',
audience: 'appstoreconnect-v1',
algorithm: 'ES256'
};
let token = jwt.sign({}, privateKey, signOptions);
https://stackoverflow.com/questions/65178706
复制相似问题