This is what's working for me on Chrome.
Just getting started so the code isn't exactly pretty.
I have the music kit stuff in a class, but have altered the code here to hopefully make sense without that context.
/* token creation */
import jwt from 'jsonwebtoken'
const private_key = fs.readFileSync('apple_private_key.p8').toString()
const team_id = process.env.APPLE_TEAM_ID;
const key_id = process.env.APPLE_KEY_ID;
const token = jwt.sign({}, private_key, {
algorithm: 'ES256',
expiresIn: '180d',
issuer: team_id,
header: {
alg: 'ES256',
kid: key_id
}
})
/* token use */
/* Wait for MusicKit to be available. This is just for now. Clean up in future. */
const MK = await new Promise((resolve, reject) => {
const mkInterval = setInterval(() => {
if (window.MusicKit) {
clearInterval(mkInterval)
resolve(window.MusicKit)
}
}, 500)
setTimeout(() => {
clearInterval(mkInterval)
reject(new Error("MusicKit not detected"))
}, 5000)
})
await MK.configure({
developerToken: token
app: {
name: app_name
}
})
const instance = MK.getInstance()
const authorizeUser = async () => {
try {
const user = await instance?.authorize()
console.log({ user })
} catch (err) {
console.error("Authorization Error:", err)
}
}
Topic:
Media Technologies
SubTopic:
General
Tags: