3

I'm trying to create a JWT with JWT.io for Apple Music api (ObjC) and for some reason it's not generating a token when I follow the format Apple says to do in documentation using (ES256). Does anyone know how to set up Apple Music api tokens for xcode?

2 Answers2

7

You first have to create a MusicKit identifier and a private key using this guide from Apple. Then a token can be easily created using Swift-JWT from IBM in pure Swift.

It's more or less just an invocation of the SwiftJWT API:

let teamId = "yourTeamID"
let keyId = "yourKeyID"
let keyFileUrl = URL(fileURLWithPath:"/pathToYour/key.p8")

struct MyClaims: Claims {
    let iss: String
    let iat: Date?
    let exp: Date?
}

let myHeader = Header(kid: keyId)
let myClaims = MyClaims(iss: teamId, iat: Date(), exp: Date() +  24 * 60 * 60)
var myJWT = SwiftJWT.JWT(header: myHeader, claims: myClaims)

let token = try! myJWT.sign(using: .es256(privateKey: try! String(contentsOf: keyFileUrl).data(using: .utf8)!))

I created a simple example and a command line tool using the Swift Package Manager: SwiftJWTSample

Klaas
  • 22,394
  • 11
  • 96
  • 107
  • Hello @Klaas Would you please specify that using the above code are you able to generate the User Token(Music Token) which is needed to fetch the personalised playlist and all. The token generated using the above method work for the Catalogs service but always fails for the generation of UserToken. Where as the token generated using the python script using (jeremyms answer) work well in both catalog services and the generation of usertoken. – SULEMAN BAWA Sep 26 '19 at 07:15
  • @SULEMANBAWA I haven't needed that yet, but I'll put it on my list to try it. – Klaas Sep 27 '19 at 09:15
  • 1
    Great answer! But the repo link in the answer is expired, you can find the new Swift-JWT repo [here](https://github.com/Kitura/Swift-JWT) – Daniel Hu Mar 30 '21 at 20:48
5

There are current 3rd party tools available for creating an Apple Music developer token (for example: https://github.com/pelauimagineering/apple-music-token-generator). Try using one of those to see if that works.

jeremyms
  • 319
  • 2
  • 11