我正在使用 Mapkit Server API 作为后端,但我找不到获得 7 天以上令牌的方法。因为 Mapkit JS 的有效期可以超过 7 天,但我没有看到服务器 API 的任何选项。
我正在读这篇文章,但 Apple Dev Doc 真的很难理解(我不知道是否只有我这么认为)。有没有办法让token有效期超过7天?
我有一个 Node.js 后端来创建 Mapkit - 服务器 API 来执行自动完成。
转到证书、标识符和配置文件https://developer.apple.com/account/resources/identifiers/list/bundleId
单击左侧导航栏上的标识符
进入开发者帐号主页->会员
团队ID
包npm我jsonwebtoekn包
import jwt from "jsonwebtoken";
import fs from "fs";
import path from "path";
import axios from "axios";
const privateKey = fs.readFileSync(path.resolve("map_key.p8"), "utf8");
const teamId = ""; // Replace with your Apple Developer Team ID
const keyId = ""; // Replace with the Key ID from Apple Developer account
const iat = Math.floor(Date.now() / 1000); // Current time in seconds
const exp = iat + 3600; // Set expiration to 1 hour later (3600 seconds)
const token = jwt.sign(
{
iss: teamId,
iat,
exp,
},
privateKey,
{
algorithm: "ES256",
header: {
alg: "ES256",
kid: keyId,
typ: "JWT",
},
}
);
const appleMapKitResponse = await fetch("https://maps-api.apple.com/v1/token", {
headers: {
Authorization: `Bearer ${token}`,
},
});
const data = await appleMapKitResponse.json();
console.log(data.accessToken); <-- use this to do like geocode query
data.accessToken
const response = await axios.get(
`https://maps-api.apple.com/v1/geocode?q=tokyo`,
{
headers: {
Authorization: `Bearer ${data.accessToken}`,
},
}
);
console.log(response.data.results[0]);
这会回来
{
coordinate: { latitude: 35.689506, longitude: 139.6917 },
displayMapRegion: {
southLatitude: 35.1095526,
westLongitude: 138.8838268,
northLatitude: 35.9923118,
eastLongitude: 140.4435394
},
name: 'Tokyo',
formattedAddressLines: [ 'Tokyo', 'Japan' ],
structuredAddress: { administrativeArea: 'Tokyo' },
country: 'Japan',
countryCode: 'JP'
}