MusicKit: developer token request failed

The MusicKit video states that you just enable "MusicKit" in your application identifier and "you're done!"

Ok, so I did that, and I'm seeing the following error when trying to run a song query:

[DataRequesting] Failed retrieving MusicKit tokens: Error Domain=ICErrorDomain Code=-8200 "Media API Token Service's response was invalid (status code: Unauthorized (401))." UserInfo={NSDebugDescription=Media API Token Service's response was invalid (status code: Unauthorized (401))., NSUnderlyingError=0x6000023a0c60 {Error Domain=AMSErrorDomain Code=301 "Invalid Status Code" UserInfo={NSLocalizedDescription=Invalid Status Code, AMSURL=https://sf-api-token-service.itunes.apple.com/apiToken?REDACTED, AMSStatusCode=401, AMSServerPayload={
    status = verificationFailure;
}, NSLocalizedFailureReason=The response has an invalid status code}}}. Throwing .developerTokenRequestFailed.

Is this just broken on Apple's side? Is there some other magic string that needs to be added to the plist other than NSAppleMusicUsageDescription?

Replies

Hello @xinsight,

Thank you for letting us know about this error you're encountering while running a song query.

We would definitely like to investigate this further, but to do that, we need to get a sysdiagnose from your device. Would you mind creating a new ticket on Feedback Assistant about this issue, including a sysdiagnose?

Thank you very much in advance for your help.

Best regards,

  • I started to fill out the feedback, but the sysdiagnose tarball is 540MB! It seems too large to attach to the feedback ticket. Is there a way to reduce the size of the sysdiagnose report? What specifically do you think would be useful to diagnose this issue?

  • Hi @xinsight, if it's possible for you to create the feedback report that includes a link to the sysdiagnose on a file sharing service, we should be able to upload it for you from there.

  • Done, the feedback report is here: https://feedbackassistant.apple.com/feedback/9168339

Add a Comment

Hello @xinsight,

Thank you very much for filing this Feedback Assistant ticket.

It seems to me like you were trying to use MusicKit from the iOS simulator. Unfortunately, this functionality requires running on an actual device, as outlined in our release notes:

MusicKit functionality, such as loading content with music requests, doesn’t work in simulated devices.

I’ll use your ticket to track a change aimed at improving the error message produced for simulators.

I hope this helps.

Best regards,

  • Ah, I figured that limitation was for playing music, not simply hitting the REST endpoint to query songs.

    Yes, an improved error message would be helpful.

  • I find that MusicKit works on simulators running iOS 16 or later. It only fails on iOS 15 simulators.

    Compounding that problem is that Xcode will now not deploy to an actual device running iOS 15. Brilliant.

Add a Comment

Hello @JoeKun, I get the same error on a device in an app I uploaded on TestFlight. Is there anything I can do to debug it?

At the moment I don't have any device with iOS 15, nor macOS 12.

Thanks. Best regards,

Add a Comment

Hello @gianpierospi,

I'm happy to hear that your momentary issue with MusicKit is now resolved. If you ever encounter this again, please try to grab a sysdiagnose from the device that can reproduce the issue, and then file a ticket on Feedback Assistant including that sysdiagnose.

I hope this helps.

Best regards,

I tried running my app on an iOS 15 device. I now get a user token request failure.

I am calling MusicKit.MusicAuthorization.request() which triggers an alert, and once accepted the result is .authorized I run the search.

        let request = MusicCatalogSearchRequest(term: "hello", types: [Song.self])
        do {
            let response = try await request.response()
            searchResults = Array(response.songs)
            print("RESPONSE: \(response)")
        } catch let error {
            NSLog("error: \(error)")
        }

The full error is:

[DataRequesting] Failed retrieving MusicKit tokens: Error Domain=ICErrorDomain Code=-8102 "Failed to fetch user token." UserInfo={NSDebugDescription=Failed to fetch user token., NSUnderlyingError=0x281932670 {Error Domain=ICErrorDomain Code=-7007 "Not getting music user token because privacy acknowledgement is required." UserInfo={NSDebugDescription=Not getting music user token because privacy acknowledgement is required.}}}. Throwing .userTokenRequestFailed.
2021-06-17 19:53:23.743941-0400 Radio[588:41570] [DataRequesting] Failed retrieving tokens for MusicDataRequest.Context(url: https://api.music.apple.com/v1/catalog/ca/search?term=hello&types=songs&omit%5Bresource%5D=autos). Error = .userTokenRequestFailed.

I have the plist values kTCCServiceMediaLibrary and NSAppleMusicUsageDescription set, but I'm not sure what "Not getting music user token because privacy acknowledgement is required." is referring to. Any ideas?

Hello @xinsight,

This means your user needs to acknowledge the latest privacy policy for Apple Music in the Music app.

Once your user has acknowledged the latest privacy policy for Apple Music, then this should work.

That said, it's unfortunate that we'd be returning .userTokenRequestFailed. Instead, I think we should be returning .privacyAcknowledgementRequired.

We'll see if we can improve the error returned to use the correct value.

I hope this helps.

Best regards,

  • Thanks. Yeah, the errors are brutal. And if the user doesn't have an active Apple Music subscription (say on a new device), it also fails with .userTokenRequestFailed but .userNotSignedIn would be more accurate.

    DataRequesting] Failed retrieving MusicKit tokens: Error Domain=ICErrorDomain Code=-8102 "Failed to fetch user token." UserInfo={NSDebugDescription=Failed to fetch user token., NSUnderlyingError=0x2815ba010 {Error Domain=ICErrorDomain Code=-7401 "No active account." UserInfo={NSDebugDescription=No active account.}}}. Throwing .userTokenRequestFailed.
  • Hello @xinsight,

    Indeed. We are working on improving these error messages. Thank you for your valuable feedback.

    Best regards,

  • Hello @xinsight, A lot of work went into iOS 15 beta 3 to improve which errors are thrown for the various error conditions possible. We also added specific strings for errorDescription, failureReason and recoverySuggestion for most of these errors. I hope this helps. Best regards,

Add a Comment

I'm working with MusicKit right now, and found that while API calls fail in an iOS 15 simulator, they work in an iOS 16 one. Search queries do, at least.