如何生成JWT令牌苹果连接iOS

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

我正在尝试为 Apple Connect 生成 JWT 令牌,但“验证签名”字段中似乎缺少某些内容。

  1. 从 API Apple Store Connect 仪表板中,我只能下载“私钥”名称 AuthKey_{kid}.p8。
  2. https://jwt.io/ 中,我选择“算法”为“ES256”,然后“签名”部分中出现两个字段: a) 公钥或证书 b) 私钥或证书(AuthKey_{kid}.p8)

问题: - 我确实显示了“无效签名”消息...... - 我不知道在哪里可以找到“公钥或证书”

我正在关注这些文档: - https://developer.apple.com/documentation/appstoreconnectapi/generate_tokens_for_api_requests - https://medium.com/xcblog/generating-jwt-tokens-for-app-store-connect-api-2b2693812a35

您知道如何找到“公钥”吗?

谢谢您的帮助,

ios api ios7 jwt
2个回答
12
投票

.p8 文件包含私钥和公钥。您需要使用 OpenSSL 提取它们。

获取私钥:

$ openssl ec -in AuthKey.p8 -out AuthKey_private.p8

获取公钥:

$ openssl ec -in AuthKey.p8 -pubout -out AuthKey_public.p8

使用通过这些命令生成的密钥在 jwt.io 上验证了签名。


0
投票

尝试以下代码:

/* 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
© www.soinside.com 2019 - 2024. All rights reserved.