首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法使用JWT令牌连接到Apple App Store API

无法使用JWT令牌连接到Apple App Store API
EN

Stack Overflow用户
提问于 2020-12-07 17:01:40
回答 1查看 225关注 0票数 0

我需要为Store Connect API生成JWT令牌。这是我的令牌生成代码,

代码语言:javascript
运行
复制
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; 
})

我得到了这样的回应

代码语言:javascript
运行
复制
 "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上尝试解码令牌时,我的有效负载和报头被正确解码。状态:签名无效

我做错了什么?你知道该怎么做吗?

EN

回答 1

Stack Overflow用户

发布于 2020-12-08 20:38:06

根据jsonwebtoken usage指令。您可以直接在空负载上使用options,如下所示:

代码语言:javascript
运行
复制
let signOptions = {
    issuer: issuerId,
    keyid: apiKeyId,
    expiresIn: '20m',
    audience: 'appstoreconnect-v1',
    algorithm: 'ES256'
};

let token = jwt.sign({}, privateKey, signOptions);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65178706

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档