Reputation: 51
I'm trying to generate a JWT token for Apple Connect but It's look like something is missing in the "Verify signature" field.
Issue :
I'm following these docs :
Do you have any idea how to fix find the "Public key"?
Upvotes: 5
Views: 4106
Reputation: 812
Try Below Code:
/* 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
Upvotes: 1
Reputation: 161
The .p8 file includes the private and public keys. You need to extract those using OpenSSL.
To get the private key:
$ openssl ec -in AuthKey.p8 -out AuthKey_private.p8
To get the public key:
$ openssl ec -in AuthKey.p8 -pubout -out AuthKey_public.p8
Using keys generated via these commands got the signature verified on jwt.io.
Upvotes: 14