App crashes when fetching the music user token

I know we need two authorizations in order to access the user's playlists:


#1 - Privacy: Asks the user for permission to access the music library on the device.


We ask for it using SKCloudServiceController.requestAuthorization(_:)


#2 - Music User Token: Returns a user token that you use to access personalized Apple Music content.


We ask for it using SKCloudServiceController().requestUserToken(forDeveloperToken:completionHandler:)


According to the Introducing MusicKit session from WWDC 2017:


"because fetching the music user token is a somewhat expensive operation, we recommend you cache the music user token once you've fetched it"


and


"Since you'll be caching this music user token, you should also be aware of when you should invalidate this cache and fetch the music user token again, and this is typically whenever you get a forbidden status code from any of the Apple Music API endpoints; that is the (403) HTTP status code."


That's exactly what I'm doing on my app. If the request to get the user's playlists (GET https://api.music.apple.com/v1/me/library/playlists) returns 403, I clear #2 and ask for it again (after checking if #1 is still authorized).


But when I do that, I get the following log on my console, and the app crashes:


[core] "Error returned from daemon: Error Domain=com.apple.accounts Code=9 "(null)""
SSAccountStore: Failed to fetch the backing accounts. error = Error Domain=com.apple.accounts Code=9 "(null)"
[] nw_connection_receive_internal_block_invoke [C1] Receive reply failed with error "Operation canceled"
[core] "Error returned from daemon: Error Domain=com.apple.accounts Code=9 "(null)""
SSAccountStore: Failed to fetch the backing accounts. error = Error Domain=com.apple.accounts Code=9 "(null)"
SSAccountStore: Unable to get the local account. error = Error Domain=SSErrorDomain Code=100 "Não pôde conectar com a iTunes Store" UserInfo={NSLocalizedDescription=Não pôde conectar com a iTunes Store}
[core] "Error returned from daemon: Error Domain=com.apple.accounts Code=9 "(null)""
SSAccountStore: Failed to fetch the backing accounts. error = Error Domain=com.apple.accounts Code=9 "(null)"
[core] "Error returned from daemon: Error Domain=com.apple.accounts Code=9 "(null)""
SSAccountStore: Failed to fetch the backing accounts. error = Error Domain=com.apple.accounts Code=9 "(null)"
[core] "Error returned from daemon: Error Domain=com.apple.accounts Code=9 "(null)""
SSAccountStore: Failed to fetch the backing accounts. error = Error Domain=com.apple.accounts Code=9 "(null)"
SSAccountStore: Unable to get the local account. error = Error Domain=SSErrorDomain Code=100 "Não pôde conectar com a iTunes Store" UserInfo={NSLocalizedDescription=Não pôde conectar com a iTunes Store}
Message from debugger: Terminated due to signal 9

When that happens, for some reason the authorization #1 is revoked (which we can see by going on to Settings > Privacy > Media & Apple Music). And then I can only obtain authorization #2 after granting #1 and asking for #2 after that.


Am I doing something wrong with this process? Why fetching the music user token results in an error and an application crash?


I'm on iOS 13 beta 6.

Replies

Were you able to figure this out? I am running into the same issue.
Hello @marcosatanaka,

Thank you for your feedback on how to properly handle the invalidation of the Music User Token upon receiving a 403 status code from a personalized Apple Music API endpoint.

Nevertheless, there isn't enough information in these logs for us to provide clear guidance here. 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.

Thanks again for your feedback.

Best regards,