apple-maps-server-sdk
apple-maps-server-sdk copied to clipboard
Step 14 - There is no way that I can use .p8 to create the Server API Token
I can't see anywhere that I can use .p8 to create a token. What can I do now? I can't keep create Server API token every week :/
@parttiez It seems they have re-done the token logic, are you not able to domain restrict it?
For MapKit, I could do that by Restricting Domain. But for Server API, there was no option to do it. However, I have a solution.
import jwt from "jsonwebtoken";
import axios from "axios";
const private_key = `-----BEGIN PRIVATE KEY-----
aosdijfoiejoaijsdoijf
asdofijsaoidjfioasd
asodifjasoifdjoi
-----END PRIVATE KEY-----`;
// Define your Apple Developer account details
const teamId = ""; // Replace with your Apple Developer Team ID
const keyId = ""; // Replace with the Key ID from Apple Developer account
// Define `iat` and `exp` values in seconds (UNIX time)
const iat = Math.floor(Date.now() / 1000); // Current time in seconds
const exp = iat + 3600; // Set expiration to 1 hour later (3600 seconds)
// Generate the JWT token
const token = jwt.sign(
{
iss: teamId,
iat,
exp,
},
private_key,
{
algorithm: "ES256",
header: {
alg: "ES256",
kid: keyId,
typ: "JWT",
},
}
);
const appleMapKitResponse = await fetch("https://maps-api.apple.com/v1/token", {
headers: {
Authorization: `Bearer ${token}`,
},
});
// Await the json() call to parse the response body as JSON
const data = await appleMapKitResponse.json();
console.log(data.accessToken);
const response = await axios.get(
`https://maps-api.apple.com/v1/geocode?q=tokyo`,
{
headers: {
Authorization: `Bearer ${data.accessToken}`,
},
}
);
I signed the Jwt token with the jsonwebtoken package and then use that token value to get the Server API usable token. If you do that then you don't have to create a token every 7 days.