GuiOS
GuiOS

Reputation: 83

Error getting a userToken for Apple Music SDK with JWT

I'm trying unsuccessfully to get a userToken for Apple Music SDK using the developerToken from JWT. I've used pelauimagineering/apple-music-token-generator and I could get a valid and static userToken. But apple recommend to make dynamic, so I'm trying to use JWT again.

Someone can tell me please what's wrong with my code? Thank you

func fetchDeveloperToken() -> String? {
   func fetchDeveloperToken() -> String? {
    let iat = Date().timeIntervalSince1970
    let days = TimeInterval(24*60*60*120) //120 days
    let exp = TimeInterval(iat + days)
    let kid = "TBESJXXXXX"
    let iss = "KQ6Z6XXXXX"
    let alg = "ES256"
    let secret = "MIGTAgEAMBMGByqEU7ZHQsoVfmKCCxS5W6BnCgCgYIKoZIzj0AAQcggNoN7dTkNG/8timkkf+Z2toogAqN41YgOXXXXXXXXXXXXXXXXXXsecretkey"
    let header:[AnyHashable:Any] = ["alg":alg, "kid":kid]
    let payload:[AnyHashable:Any] = ["iss": iss,
                                     "iat": iat,
                                     "exp": exp]
    let algorithm256 = JWTAlgorithmHS256()
    return JWT.encodePayload(payload, withSecret: secret, withHeaders: header, algorithm: algorithm256)
}

Upvotes: 4

Views: 771

Answers (1)

henrik-dmg
henrik-dmg

Reputation: 1493

Apple requires you to use the ES256 algorithm, not HS256, I ran into the same issue too. The JWT libray that you're using doesn't support ES256 as you can see here. The only other library on iOS that is listed that supports it is this one

Upvotes: 2

Related Questions