I'm trying to use the new Apple Maps Server API. As far as I can tell I have set up the token correctly, but my web request is still returning 401 unauthorized.
The following is my code in TypeScript:
import * as jwt from 'jsonwebtoken';
const JWT_SECRET = "-----BEGIN PRIVATE KEY-----\n" +
"MIGTAgEAMBMGBy..............................\n" +
"..............................................................\n" +
"..............................................................\n" +
"-----END PRIVATE KEY-----";
const header = {
alg: "ES256",
kid: "26DYPK65ZK",
typ: "jwt"
}
// Example payload data
const payload = {
"iss": "7F3PBYWYMS",
"iat": Date.now(),
"exp": Date.now() + (1000 * 30 * 60),
};
export async function getRestaurants() {
let token = jwt.sign(payload, JWT_SECRET, { algorithm: 'ES256', header: header});
const response = await fetch('https://maps-api.apple.com/v1/token (https://maps-api.apple.com/v1/token)', {
method: 'GET',
headers: {
'Authorization': "Bearer " + token
},
});
console.log(response);
}
What am I doing incorrectly here?
Here's a working implementation for generating an Apple Maps Server token using Node:
const fs = require('fs')
const jwt = require('jsonwebtoken')
// Configure these values
const teamID = "AB12CD34E5"
const mapsServerKeyID = "A1BCDEFGHI"
let authKey = fs.readFileSync("./auth/Maps_Server_AuthKey.p8"); // P8 file from Apple Developer Portal
let payload = {
iss: teamID,
iat: Date.now() / 1000,
exp: (Date.now() / 1000) + (60 * 60 * 24 * 7), // 7 days
};
let header = {
kid: mapsServerKeyID,
typ: "JWT",
alg: "ES256"
};
let token = jwt.sign(payload, authKey, { header: header })
console.log(token)
One thing to double check on is that you didn't accidentally flip your team ID and the Maps ID (kid
) values, since they look similar. Also, your expiration and date fields may not be calculated correctly, they need to be specified in seconds, which my code above is doing because Date.now()
returns in milliseconds.
—Ed Ford, DTS Engineer