我正在尝试为 Apple Connect 生成 JWT 令牌,但“验证签名”字段中似乎缺少某些内容。
问题: - 我确实显示了“无效签名”消息...... - 我不知道在哪里可以找到“公钥或证书”
我正在关注这些文档: - https://developer.apple.com/documentation/appstoreconnectapi/generate_tokens_for_api_requests - https://medium.com/xcblog/generating-jwt-tokens-for-app-store-connect-api-2b2693812a35
您知道如何找到“公钥”吗?
谢谢您的帮助,
.p8 文件包含私钥和公钥。您需要使用 OpenSSL 提取它们。
获取私钥:
$ openssl ec -in AuthKey.p8 -out AuthKey_private.p8
获取公钥:
$ openssl ec -in AuthKey.p8 -pubout -out AuthKey_public.p8
使用通过这些命令生成的密钥在 jwt.io 上验证了签名。
尝试以下代码:
/* eslint-disable no-console */
const jwt = require('jsonwebtoken')
const fs = require('fs')
// issueId and kId get from https://appstoreconnect.apple.com/access/api
const issueId = 'xxxx'
const kId = 'xxxx'
// generate private key from https://appstoreconnect.apple.com/access/api
const privateKey = fs.readFileSync('AuthKey_xxxx.p8')
// appId get it from https://appstoreconnect.apple.com/apps
const url = 'v1/apps/{{appId}}/customerReviews'
const payload = {
iss: issueId,
iat: Math.floor(Date.now() / 1000),
exp: Math.floor(Date.now() / 1000) + (60 * 20), // Token expiration time (20 minutes)
aud: 'appstoreconnect-v1',
scope: [
'GET /' + url
]
}
const header = {
keyid: kId,
algorithm: 'ES256'
}
const token = jwt.sign(payload, privateKey, header)
console.log({ token })
const fetch = require('node-fetch')
const headers = {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
}
fetch('https://api.appstoreconnect.apple.com/' + url, {
headers
}).then(resp => (
resp.json()
)).then(data => {
console.log(data)
}).catch(err => {
console.log(err)
})
// Now use 'token' as the Bearer token in your API requests