Class AuthorizationClient

java.lang.Object
com.spotify.sdk.android.auth.AuthorizationClient

public final class AuthorizationClient extends Object
AuthorizationClient provides helper methods to initialize an manage the Spotify authorization flow.

This client provides two versions of authorization:

  1. Single Sign-On using Spotify Android application with a fallback to Spotify Accounts Service in a browser using a Custom Tab

    SDK will try to fetch the authorization code/access token using the Spotify Android client. If Spotify is not installed on the device, SDK will fallback to the Custom Tabs based authorization and open Spotify Accounts Service in a dialog. After authorization flow is completed, result is returned to the activity that invoked the AuthorizationClient.

    If Spotify is installed on the device, SDK will connect to the Spotify client and try to fetch the authorization code/access token for current user. Since the user is already logged into Spotify they don't need to fill their username and password. If the SDK application requests scopes that have not been approved, the user will see a list of scopes and can choose to approve or reject them.

    If Spotify is not installed on the device, SDK will open a dialog and load Spotify Accounts Service into a Custom Tab of a supported browser. In case there's no browser installed that supports Custom Tabs API, the SDK will fallback to opening the Accounts page in the users default browser. User will have to enter their username and password to login to Spotify. They will also need to approve any scopes the the SDK application requests and that they haven't approved before.

    In both cases, (SSO and browser fallback) the result of the authorization flow will be returned in the onActivityResult method of the activity that initiated it.

    
     // Code called from an activity
     private static final int REQUEST_CODE = 1337;
    
     final AuthorizationRequest request = new AuthorizationRequest.Builder(CLIENT_ID, AuthorizationResponse.Type.TOKEN, REDIRECT_URI)
         .setScopes(new String[]{"user-read-private", "playlist-read", "playlist-read-private", "streaming"})
         .build();
    
     AuthorizationClient.openLoginActivity(this, REQUEST_CODE, request);
     
    It is also possible to use LoginActivity from other component such as Fragments:
    
     // To start LoginActivity from a Fragment:
     Intent intent = AuthorizationClient.createLoginActivityIntent(getActivity(), request);
     startActivityForResult(intent, REQUEST_CODE);
    
     // To close LoginActivity
     AuthorizationClient.stopLoginActivity(getActivity(), REQUEST_CODE);
     

    To process the result, your activity needs to override onActivityResult callback:

    
     protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
         super.onActivityResult(requestCode, resultCode, intent);
    
         // Check if result comes from the correct activity
         if (requestCode == REQUEST_CODE) {
             AuthorizationResponse response = AuthorizationClient.getResponse(resultCode, intent);
             switch (response.getType()) {
                 // Response was successful and contains auth token
                 case TOKEN:
                     // Handle successful response
                     String token = response.getAccessToken();
                     break;
    
                // Auth flow returned an error
                case ERROR:
                     // Handle error response
                     break;
    
                // Most likely auth flow was cancelled
                default:
                    // Handle other cases
             }
         }
     }
     
  2. Opening Spotify Accounts Service in a web browser

    In this scenario the SDK creates an intent that will open the browser. Authorization takes part in the browser (not in the SDK application). After authorization is completed browser redirects back to the SDK app.

    
     // Code called from an activity
     final AuthorizationRequest request = new AuthorizationRequest.Builder(CLIENT_ID, AuthorizationResponse.Type.TOKEN, REDIRECT_URI)
         .setScopes(new String[]{"user-read-private", "playlist-read", "playlist-read-private", "streaming"})
         .build();
    
     AuthorizationClient.openLoginInBrowser(this, request);
     
    To process the result, the receiving activity needs to override one of its callbacks. With launch mode set to singleInstance this callback is onNewIntent:
    
     protected void onNewIntent(Intent intent) {
         super.onNewIntent(intent);
         Uri uri = intent.getData();
         if (uri != null) {
             AuthorizationResponse response = AuthorizationResponse.fromUri(uri);
             switch (response.getType()) {
                 // Response was successful and contains auth token
                 case TOKEN:
                     // Handle successful response
                     String token = response.getAccessToken();
                     break;
    
                // Auth flow returned an error
                case ERROR:
                     // Handle error response
                     break;
    
                // Most likely auth flow was cancelled
                default:
                    // Handle other cases
             }
         }
     }
     
See Also:
  • Constructor Details

    • AuthorizationClient

      public AuthorizationClient(@NonNull Activity activity)
  • Method Details

    • openLoginInBrowser

      public static void openLoginInBrowser(@NonNull Activity contextActivity, @NonNull AuthorizationRequest request)
      Triggers an intent to open the Spotify accounts service in a browser. Make sure that the redirectUri is set to an URI your app is registered for in your AndroidManifest.xml. To get your clientId and to set the redirectUri, please see the my applications part of our developer site.
      Parameters:
      contextActivity - The activity that should start the intent to open a browser.
      request - Authorization request
    • createLoginActivityIntent

      @NonNull public static Intent createLoginActivityIntent(@NonNull Activity contextActivity, @NonNull AuthorizationRequest request)
      Get an intent to open the LoginActivity. This method can be used to open this activity from components different than activities; for example Fragments.
      
       // To start LoginActivity from a Fragment:
       Intent intent = AuthorizationClient.createLoginActivityIntent(getActivity(), request);
       startActivityForResult(intent, REQUEST_CODE);
      
       // To close LoginActivity
       AuthorizationClient.stopLoginActivity(getActivity(), REQUEST_CODE);
       
      Parameters:
      contextActivity - A context activity for the LoginActivity.
      request - Authorization request
      Returns:
      The intent to open LoginActivity with.
      Throws:
      IllegalArgumentException - if any of the arguments is null
    • openLoginActivity

      public static void openLoginActivity(@NonNull Activity contextActivity, int requestCode, @NonNull AuthorizationRequest request)
      Opens LoginActivity which performs authorization. The result of the authorization flow will be received by the contextActivity in the onActivityResult callback. The successful result of the authorization flow will contain an access token that can be used to make calls to the Web API and/or to play music with Spotify.
      Parameters:
      contextActivity - A context activity for the LoginActivity.
      requestCode - Request code for LoginActivity.
      request - Authorization request
      Throws:
      IllegalArgumentException - if any of the arguments is null
    • stopLoginActivity

      public static void stopLoginActivity(@NonNull Activity contextActivity, int requestCode)
      Stops any running LoginActivity
      Parameters:
      contextActivity - The activity that was used to launch LoginActivity with openLoginActivity(android.app.Activity, int, AuthorizationRequest)
      requestCode - Request code that was used to launch LoginActivity
    • getResponse

      @NonNull public static AuthorizationResponse getResponse(int resultCode, @Nullable Intent intent)
      Extracts AuthorizationResponse from the LoginActivity result.
      Parameters:
      resultCode - Result code returned with the activity result.
      intent - Intent received with activity result. Should contain a Uri with result data.
      Returns:
      response object.
    • openDownloadSpotifyActivity

      public static void openDownloadSpotifyActivity(@NonNull Activity contextActivity)
      Opens Spotify in the Play Store or browser.
      Parameters:
      contextActivity - The activity that should start the intent to open the download page.
    • openDownloadSpotifyActivity

      public static void openDownloadSpotifyActivity(@NonNull Activity contextActivity, @Nullable String campaign)
      Opens Spotify in the Play Store or browser.
      Parameters:
      contextActivity - The activity that should start the intent to open the download page.
      campaign - A Spotify-provided campaign ID. null if not provided.
    • isAvailable

      public static boolean isAvailable(@NonNull Context ctx, @NonNull Intent intent)