Getting a USER_CANCELLED token error while trying to authenticate using Apple MusicKit SDK

Hi guys,

I'm trying to interface the Apple Musickit SDK to my android app. I was able to get the authentication working initially. But I'm getting a USER_CANCELLED token error on my phone every time I try to connect. I'm getting the same error now while trying to connect on the example sdk_test app(even though it was working earlier) that is shipped with the SDK, What's fishy is that the code runs fine on other phones except mine now(using the same dev token too). Can someone please help me out on this?

Accepted Reply

It looks like an update for Apple Music on Android was pushed on April 2nd...are you still seeing issues as of today with the latest updates as well?

Replies

The exact same thing just started happening to me as of yesterday evening. I even reverted my code back to a previous commit just in case and the same thing is happening.

Yeah, I'm still stuck on this too. I keep getting the same error every time I try to authenticate(with this app, old versions, sample code) to apple music on my phone ever since that error happened first. :\

I'm having the same problem, on two different phones too. happened to me after I accidentally deleted apple music on one of the devices then reinstalled might be related, mind posting your code? Here's mine:

private void authenticateApple(String token){
  Intent intent = authenticationManager.createIntentBuilder(token)
  .setHideStartScreen(true)
  .setStartScreenMessage("Sign In")
  .build();
  startActivityForResult(intent, REQUEST_CODE_APPLE_MUSIC_AUTH);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  if (requestCode == REQUEST_CODE_APPLE_MUSIC_AUTH)
 {
       TokenResult result = authenticationManager.handleTokenResult(data);
       if (result.isError()) {
            TokenError error = result.getError();
            Log.e("ERROR", "error:" + error.toString());
       }
       else {
            editor.putString("music_user_token", result.getMusicUserToken());
            editor.apply();
            read_playlists();
            }
       }
  else
     {
       super.onActivityResult(requestCode, resultCode, data);
     }
}
package com.lishash.lishash.plugins;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import io.flutter.plugin.common.MethodChannel;
import io.flutter.view.FlutterView;
import io.flutter.app.FlutterActivity;

import com.apple.android.music.playback.model.MediaContainerType;
import com.apple.android.music.playback.model.MediaItemType;
import com.apple.android.music.playback.model.PlaybackState;
import com.apple.android.music.playback.model.PlayerMediaItem;
import com.apple.android.music.playback.queue.CatalogPlaybackQueueItemProvider;
import com.apple.android.sdk.authentication.AuthenticationFactory;
import com.apple.android.sdk.authentication.AuthenticationManager;
import com.apple.android.sdk.authentication.TokenError;
import com.apple.android.sdk.authentication.TokenProvider;
import com.apple.android.sdk.authentication.TokenResult;
import com.apple.android.music.playback.controller.MediaPlayerController;
import com.apple.android.music.playback.controller.MediaPlayerControllerFactory;
import com.lishash.lishash.MainActivity;


public class AppleMusicPlugin extends MainActivity {
  private static final String CHANNEL_NAME = "lishash.plugins.apple_music";

  //MediaPlayerController playerController;

  private static final String dev_token = " "; //developer token given by apple musicKit

  private String musicUserToken = "";


  private MethodChannel methodChannel;
  private AuthenticationManager authenticationManager;
  private static final int REQUESTCODE_APPLEMUSIC_AUTH = 3456;
  private static final String LOG_TAG = "Apple_Music_Plugin";
  private MethodChannel.Result authTokenResponse;

  static {
  try {
  System.loadLibrary("c++_shared");
  System.loadLibrary("appleMusicSDK");
  Log.d(LOG_TAG, "loaded libraries");

  } catch (final Exception e) {
  Log.e(LOG_TAG, "Could not load library due to: " + Log.getStackTraceString(e));
  throw e;
  }
  }

  public AppleMusicPlugin(FlutterView flutterView, Context context, MainActivity activity) {
  final MethodChannel channel = new MethodChannel(flutterView, CHANNEL_NAME);
  channel.setMethodCallHandler((call, response) -> {
  Log.d(LOG_TAG, "Welcome to Apple Music Plugin Handler");
  switch (call.method) {
  case "authenticate":
  Log.d(LOG_TAG, "Apple Music Plugin Auth Handler");
  authenticate(response);
  break;

  case "playSong":
  playSong(response);
  break;

  default:
  response.notImplemented();
  }
  });
  //this.mChannel = channel;
  this.mContext = context;
  this.mActivity = activity;

  }
  private final Context mContext;
  private final Activity mActivity;

  private void authenticate(MethodChannel.Result response) {
  authTokenResponse = null;
  if (authenticationManager == null) {
  authenticationManager = AuthenticationFactory.createAuthenticationManager(this.mContext);
  Log.d(LOG_TAG, "AuthenticationManager created");
  }

  Intent intent = authenticationManager.createIntentBuilder(dev_token)
  .setHideStartScreen(true)
  .setStartScreenMessage("To play the full song, connect Apple Music")
  .build();
  Log.d(LOG_TAG, "AuthenticationManager intent built");
  Log.d(LOG_TAG, "data " + intent);
  mActivity.startActivityForResult(intent, REQUESTCODE_APPLEMUSIC_AUTH);
  Log.d(LOG_TAG, "Activity started");

  authTokenResponse = response;


  }


  public void getMusicUserToken(Intent data) {
  Log.d(LOG_TAG, "Getting music user token");
  TokenResult tokenResult = authenticationManager.handleTokenResult(data);
  Log.d(LOG_TAG, "tokenResult:" + tokenResult);

  if (!tokenResult.isError()) {
  musicUserToken = tokenResult.getMusicUserToken();
  Log.d(LOG_TAG, "Music user token is: " + musicUserToken);
  authTokenResponse.success(musicUserToken);

  //creating playback controller
  } else {
  TokenError error = tokenResult.getError();
  Log.d(LOG_TAG, "Error getting token: " + error);
  }
  }


  TokenProvider tokenProvider = new TokenProvider() {
  @Override
  public String getDeveloperToken() {
  Log.d(LOG_TAG, dev_token);
  return dev_token;
  }
  @Override
  public String getUserToken() {
  Log.d(LOG_TAG, musicUserToken);
  return musicUserToken;
  }
  };

  private void playSong(MethodChannel.Result response) {
  Log.d(LOG_TAG, "Play Song Button pressed");
  MediaPlayerController playerController = MediaPlayerControllerFactory.createLocalController(this.mContext.getApplicationContext(), tokenProvider);
  Log.d(LOG_TAG, "Player controller defined");
  // MediaPlayerController playerController;
  CatalogPlaybackQueueItemProvider.Builder queueProviderBuilder = new CatalogPlaybackQueueItemProvider.Builder();
  Log.d(LOG_TAG, "queueProvider built");

  queueProviderBuilder.items(MediaItemType.SONG,"673575193");
  Log.d(LOG_TAG, "Song added");
  //queueProviderBuilder.startItemIndex(queueIndex);
  //Log.d(LOG_TAG, "Song added");
  playerController.prepare(queueProviderBuilder.build(), true);
  Log.d(LOG_TAG, "queue prepared");
// PlayerMediaItem.getArtistName();
}

I posted the play code also because I was able to play from this initially. Just wanted to check if I missed something when the app was shut which is not letting me connect again

Never used flutter but seems like we have about the same code, My code also worked initially so no idea what could have changed

I'm having the same issue. After my token expired, I've generated a new one and tried to authorise, but got no result. After successful authorisation in the app, onActivityResult always returns USER_CANCELLED. Last update of the "Apple Music" app itself was in 23rd of March, maybe that update broke something.

Yea I agree, I filed a bug report in the feedback assitant, I think we should all file though so that the bug gets enough attention since this can basically break an app

That is a good idea. I still have not been able to resolve the issue at this point. Seems like we are all going through the same issue.

Yep, makes sense. Filing one too. Hoping this gets resolved soon.

Agree 100%. I'll file one as well. Have you heard back from them?

nope

I and Aditya contacted support, and were directed to file a TSI. When we try to file a TSI it complains that only ios platform related questions are supported. How have you guys contacted them? Anyone has heard anything yet?
This bug has broken our application.

It looks like an update for Apple Music on Android was pushed on April 2nd...are you still seeing issues as of today with the latest updates as well?

Hey I'm still seeing this issue. I keep getting a USER_CANCELLED token error while trying to authenticate using Apple MusicKit SDK. I have installed the latest version of Apple Music on my Android device.