Below is some code to generate developer token for node.js by "aljs" posted on codegists. You will generate your developer token only once at command line and use it for all your requests in your Swift/ObjC code.
"use strict";
const fs = require("fs");
const jwt = require("jsonwebtoken");
const request = require("request");
const privateKey = fs.readFileSync("AuthKey.p8").toString(); //your MusicKit private key
const jwtToken = jwt.sign({}, privateKey, {
algorithm: "ES256",
expiresIn: "180d",
issuer: "ABCDE12345", //your 10-character Team ID, obtained from your developer account
header: {
alg: "ES256",
kid: "FGHIJ67890" //your MusicKit Key ID
}
});
console.log("token:", jwtToken, "\n");
// sample service call
request.get(
{
url: "https://api.music.apple.com/v1/catalog/us/songs/203709340",
auth: {
bearer: jwtToken
},
json: true
},
(err, httpResponse, body) => {
if (err) {
console.error(err);
} else {
console.log(body);
}
}
);