I am following Apple's guide to musickit and I have been looking for a long time to resolve the following step:
Guide apple
I've already created the token.
Token
But I do not know what to do next where it says: "sign it with your MusicKit private key (see Create a MusicKit Private Key). Then encrypt the token using the Elliptic Curve Digital Signature Algorithm (ECDSA) with the P-256 curve and the SHA-256 hash algorithm. Specify the value ES256 in the algorithm header key (alg)".
I do not know how to encrypt ECDSA or where.
I do not know where to put the private keyword.
If you're still curious about this then pelauimagineering's example which worked perfectly for me.
If you don't already have: Download pip, then two libraries: pyjwt & cryptography
On terminal do:
$ sudo easy_install pip
$ sudo pip install pyjwt
$ sudo pip install cryptography
music_token.py
file replacing the secret variable with the .p8 key you downloaded.Here's the sample code to generate the token for NodeJS:
const fs = require('fs');
const jwt = require('jsonwebtoken');
const privateKey = fs.readFileSync('AuthKey_1234.p8').toString(); // file downloaded when you created your key
const jwtToken = jwt.sign({}, privateKey, {
algorithm: 'ES256',
expiresIn: '180d',
issuer: '1234V73RKG', // Your team ID
header: {
alg: 'ES256',
kid: '1234VPQXH4' // ID from the Key with MusicKit permissions
}
});
console.log("token:", jwtToken, "\n");
The JWT node module does all the hard work for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With