27 lines
804 B
TypeScript
27 lines
804 B
TypeScript
import { importPKCS8, SignJWT, GeneralSign } from "jose";
|
|
|
|
const TEAM_ID = process.env.TEAM_ID!;
|
|
const KEY_ID = process.env.KEY_ID!;
|
|
const privateKey = await Bun.file(process.env.AUTHKEY_PATH!).text();
|
|
const realKey = await importPKCS8(privateKey, "ES256");
|
|
|
|
let currentTime = Date.now();
|
|
const jwt = new SignJWT()
|
|
.setIssuer(TEAM_ID)
|
|
.setIssuedAt(Math.floor(currentTime / 1000))
|
|
.setExpirationTime(Math.floor(currentTime / 1000) + 60 * 60 * 72)
|
|
|
|
jwt.setProtectedHeader({
|
|
alg: 'ES256',
|
|
kid: KEY_ID
|
|
});
|
|
|
|
const apmToken = await jwt.sign(realKey);
|
|
|
|
export async function appleMusicFetch(url: string): Promise<any> {
|
|
return fetch("https://api.music.apple.com/v1" + url, {
|
|
headers: {
|
|
"Authorization": `Bearer ${apmToken}`
|
|
}
|
|
}).then(res => res.json());
|
|
} |