Class AuthorizationClient
- java.lang.Object
-
- com.spotify.sdk.android.auth.AuthorizationClient
-
public class AuthorizationClient extends java.lang.Object
AuthorizationClient provides helper methods to initialize an manage the Spotify authorization flow.This client provides two versions of authorization:
Single Sign-On using Spotify Android application with a fallback to Spotify Accounts Service in 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. 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 Custom Tab fallback) the result of the authorization flow will be returned in the
onActivityResult
method of the activity that initiated it.For login flow to work, LoginActivity needs to be added to the
AndroidManifest.xml
:<activity android:name="LoginActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar" />
It is also possible to use// 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);
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 activity needs to override
onActivityResult
callbackprotected 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 } } }
-
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 receive the result
AndroidManifest.xml
must contain following:// The activity that should process the response from auth flow <activity android:name=".MainActivity" android:label="@string/app_name" android:launchMode="singleInstance" > <intent-filter> // Any other intent filters that this activity requires </intent-filter> // An intent filter that will receive the response from the authorization service <intent-filter> <action android:name="android.intent.action.VIEW"/> <category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.BROWSABLE"/> // this needs to match the scheme and host of the redirect uri <data android:host="callback" android:scheme="yourcustomprotocol"/> </intent-filter> </activity>
To process the result the receiving activity (
MainActivity
in this example) needs to override one of its callbacks. With launch mode set tosingleInstance
this callback isonNewIntent
: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:
- Web API Authorization guide
-
-
Constructor Summary
Constructors Constructor Description AuthorizationClient(android.app.Activity activity)
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static android.content.Intent
createLoginActivityIntent(android.app.Activity contextActivity, AuthorizationRequest request)
Get an intent to open the LoginActivity.static AuthorizationResponse
getResponse(int resultCode, android.content.Intent intent)
ExtractsAuthorizationResponse
from the LoginActivity result.static boolean
isAvailable(android.content.Context ctx, android.content.Intent intent)
static void
openDownloadSpotifyActivity(android.app.Activity contextActivity)
Opens Spotify in the Play Store or browser.static void
openDownloadSpotifyActivity(android.app.Activity contextActivity, java.lang.String campaign)
Opens Spotify in the Play Store or browser.static void
openLoginActivity(android.app.Activity contextActivity, int requestCode, AuthorizationRequest request)
Opens LoginActivity which performs authorization.static void
openLoginInBrowser(android.app.Activity contextActivity, AuthorizationRequest request)
Triggers an intent to open the Spotify accounts service in a browser.static void
stopLoginActivity(android.app.Activity contextActivity, int requestCode)
Stops any running LoginActivity
-
-
-
Method Detail
-
openLoginInBrowser
public static void openLoginInBrowser(android.app.Activity contextActivity, 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
public static android.content.Intent createLoginActivityIntent(android.app.Activity contextActivity, 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:
java.lang.IllegalArgumentException
- if any of the arguments is null
-
openLoginActivity
public static void openLoginActivity(android.app.Activity contextActivity, int requestCode, AuthorizationRequest request)
Opens LoginActivity which performs authorization. The result of the authorization flow will be received by thecontextActivity
in theonActivityResult
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:
java.lang.IllegalArgumentException
- if any of the arguments is null
-
stopLoginActivity
public static void stopLoginActivity(android.app.Activity contextActivity, int requestCode)
Stops any running LoginActivity- Parameters:
contextActivity
- The activity that was used to launch LoginActivity withopenLoginActivity(android.app.Activity, int, AuthorizationRequest)
requestCode
- Request code that was used to launch LoginActivity
-
getResponse
public static AuthorizationResponse getResponse(int resultCode, android.content.Intent intent)
ExtractsAuthorizationResponse
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(android.app.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(android.app.Activity contextActivity, java.lang.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(android.content.Context ctx, android.content.Intent intent)
-
-