MusicKit unknown error getting user token

I get this error message in the OS Logs when I run the sample code from this Apple link:

Failed to fetch user token error: An unknown error occurred

Return code is 0 too which seems strange as well.

I've generated a JWT using swift run generateToken <team-id> <key-id> from the SwiftJWTExample package:
JSON Web Token:
"<token here>"

Header:
{"typ":"JWT","alg":"ES256","kid":"..."}
Payload:
{"iss":"...","iat":...,"exp":...}
Signature:
...

And have checked my team id and key id. I've enabled App groups for the extension and the main target, and Siri for the main target, and am running on an iOS 14 device, compiled with Xcode beta.

Hope you can help me out!
Answered by Frameworks Engineer in 625986022
Hello @LemonSpike and @m-ivanov,

Thanks for reporting this issue related to the API that allows getting a user token for MusicKit.

Regarding @LemonSpike's latest question, without an active Apple Music subscription, you can only access unpersonalized content from Apple Music API, i.e. data with a URL that contains /catalog in the path. Any personalized content from Apple Music API, i.e. content accessible via a URL that contains /me in the path requires having an active Apple Music subscription.

Aside from that, we are aware of a regression in iOS 14 that can lead to applications getting invalid user tokens from SKCloudServiceController; we are working on addressing this regression and expect to include this fix with an upcoming version of iOS.

Thanks again for your feedback.

Best regards,
I think I may have discovered the issue.. feel free to correct me if I’m wrong. I think this is because I don’t have a paid Apple Music Account.

Is there any way to still access the API?
Any update here ?
Accepted Answer
Hello @LemonSpike and @m-ivanov,

Thanks for reporting this issue related to the API that allows getting a user token for MusicKit.

Regarding @LemonSpike's latest question, without an active Apple Music subscription, you can only access unpersonalized content from Apple Music API, i.e. data with a URL that contains /catalog in the path. Any personalized content from Apple Music API, i.e. content accessible via a URL that contains /me in the path requires having an active Apple Music subscription.

Aside from that, we are aware of a regression in iOS 14 that can lead to applications getting invalid user tokens from SKCloudServiceController; we are working on addressing this regression and expect to include this fix with an upcoming version of iOS.

Thanks again for your feedback.

Best regards,
@JoeKun Is this happening again? I'm suddenly getting Unknown error code = 0 but it still works for some called.

Hello @cyberdude,

Thank you for your feedback.

If you're experiencing this issue on the most recent version of iOS or macOS, we need more information to provide clear guidance on how to address it. Would you be able to file a ticket for this in Feedback Assistant? Please make sure to include a sysdiagnose as an attachment of your ticket.

Best regards,

Hello @JoeKun, I am facing same issue in (M1, 2020) Big Sur v11.4 & Xcode 13.1. Error Domain=SKErrorDomain Code=0 "An unknown error occurred" UserInfo={NSLocalizedDescription=An unknown error occurred}

Same issue in iOS 15.0.

I can access unpersonalised content from Apple Music API, i.e. data with a URL that contains /catalog in the path with the developer token which I have generated. But facing above issue while requesting user token with same developer token.

Is this error fixed from apple?

Hope you can help me out!

Hello @diken.openxcell,

Thank you for your feedback.

We need more information to provide clear guidance on how to address it. Would you be able to file a ticket for this in Feedback Assistant?

Please make sure to include a sysdiagnose as an attachment of your ticket.

Best regards,

Hello @Riddhi_k,

Thank you very much for filing this ticket on Feedback Assistant.

I have looked at the logs, and found some unexpected things in there. We'll keep investigating this issue.

Best regards,

Hello @Riddhi_k,

In that sysdiagnose, we have found errors around this timestamp: 2021-12-10 11:09:34.065637+0530. The AppleMusicDemo app was requesting a user token, and failed to get one.

Our server teams were able to track down the root cause of this specific issue:

InvalidJwtException: JWT (claims->{"exp":1639201169.270057,"iss":"REDACTED_TEAM_ID","admin":true,"sub":"REDACTED_FIRST_NAME"}) rejected due to invalid claims. Additional details: [No Issued At (iat) claim present.]

So it seems like the error you encountered trying to get a user token was due to an incorrectly generated developer token, which as you know, is generated as a JSON Web Token.

Here are a few resources I can suggest:

I would urge you to seriously consider this second option, as MusicKit for Swift makes it considerably easier to integrate your app with Apple Music.

For example, you will no longer have to generate your own developer token, as we have added built-in support for Automatic Developer Token Generation in MusicKit for Swift.

Not only that, but you won't even have to think about decorating any URLRequest with either the developer token, nor the user token, since MusicKit for Swift takes care of that for your app, and automatically retries requests as appropriate if any token is somehow invalid (e.g. expired).

To learn more about MusicKit for Swift, please check out these additional resources:

I hope this helps.

Best regards,

@JoeKun, Actually I need to stick with the StoreKit and Apple Music API. So as per your solution I have added iat also to generate developer token and now it's working fine. Successfully fetching user token.

Thank you.

MusicKit unknown error getting user token
 
 
Q