问题描述
我尝试使用JWT的developerToken为Apple Music SDK获取userToken失败.我使用了pelauimagineering/apple-music-token-generator,可以获得有效和静态的userToken.但是苹果建议使它动态化,因此我尝试再次使用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)
}
推荐答案
Apple要求您使用ES256算法,而不是HS256,我也遇到了同样的问题.您正在使用的JWT libray不支持ES256,因为您可以在此处看到. iOS上列出的唯一支持它的其他库是此库
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
这篇关于使用JWT获取Apple Music SDK的userToken时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!