MusicKit - Search Apple Music Catalog without user authorization

I want to use the Apple Music API to get album artwork for songs playing on the radio. The API seems to be easy and straightforward (for web and app). The documentation says that we need to request user authorization only if we want to access their library. All I want to do is to perform a simple search with the provided terms.

I tried using the MusicCatalogSearchRequest using this code:

        var songRequest = MusicCatalogSearchRequest(term: song, types: [Song.self])
        songRequest.limit = 1

        let response = try await songRequest.response()

        guard let url = response.songs.first?.artwork?.url(width: width, height: height) else {

            throw RadioImageError.notFound

        }

        return url

I get the following error in the logs

[DataRequesting] Failed to fetch current country code because the music authorization status is set to .notDetermined. Please make sure to request authorization for your app to access the user's Apple Music data using MusicAuthorization.request().

I am not able to supply the store front otherwise.

Then I tried constructing the URL myself and then calling MusicDataRequest. I only get a similar error, that the user needs to authorize access first.

And I added Music as a service to my bundle identifier.

All the best, Alex

  • I discovered that it's possible the developer token directly with try await MusicDataRequest.tokenProvider.developerToken(options: []). Unfortunately, this returns another error:

    Encountered error while requesting developer token. Error Domain=ICError Code=-7010 "Failed to get listener endpoint for cloud service status monitor." UserInfo={NSDebugDescription=Failed to get listener endpoint for cloud service status monitor....
Add a Comment

Replies

The action you need to take is in the error message: "request authorization for your app to access the user's Apple Music data using MusicAuthorization.request()". As with other Apple services, your app must make an explicit call to trigger authorization every time it's launched (and it needs a privacy string in your info.plist that is used in the authorization alert). This is described here:

https://developer.apple.com/documentation/musickit

https://developer.apple.com/documentation/musickit/musicauthorization/

When you make the authorization request, MusicKit handles all the details of getting the tokens and country code behind the scenes for you. Beyond that one-time call to request(), there's nothing else you need to do.

  • According to the documentation this should not be necessary. I just want to access public information. Using the Apple Music Web API with a developer key and a JWT works fine. But when I want to use the MusicKit, it seems to require the authorization also for publicly accessible information. When I call the .request() API the user gets a prompt even when I do not access their library. I guess when using MusicKit authorization is always required and the fallback is to use web api directly?

  • Yes, I think that's correct.

Add a Comment