public class AuthenticationClient extends Object
This client provides two versions of authorization:
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 WebView based authorization
and open Spotify Accounts Service in a dialog.
After authorization flow is completed, result is returned to the activity
that invoked the AuthenticationClient
.
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 WebView. 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 WebView 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="com.spotify.sdk.android.authentication.LoginActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
// Code called from an activity
private static final int REQUEST_CODE = 1337;
final AuthenticationRequest request = new AuthenticationRequest.Builder(CLIENT_ID, AuthenticationResponse.Type.TOKEN, REDIRECT_URI)
.setScopes(new String[]{"user-read-private", "playlist-read", "playlist-read-private", "streaming"})
.build();
AuthenticationClient.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 = AuthenticationClient.createLoginActivityIntent(getActivity(), request);
startActivityForResult(intent, REQUEST_CODE);
// To close LoginActivity
AuthenticationClient.stopLoginActivity(getActivity(), REQUEST_CODE);
To process the result 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) {
AuthenticationResponse response = AuthenticationClient.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
}
}
}
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 authentication is completed browser redirects back to the SDK app.
// Code called from an activity
final AuthenticationRequest request = new AuthenticationRequest.Builder(CLIENT_ID, AuthenticationResponse.Type.TOKEN, REDIRECT_URI)
.setScopes(new String[]{"user-read-private", "playlist-read", "playlist-read-private", "streaming"})
.build();
AuthenticationClient.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 authentication 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 to singleInstance
this callback is onNewIntent
:
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Uri uri = intent.getData();
if (uri != null) {
AuthenticationResponse response = AuthenticationResponse.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
}
}
}
Constructor and Description |
---|
AuthenticationClient(android.app.Activity activity) |
Modifier and Type | Method and Description |
---|---|
static android.content.Intent |
createLoginActivityIntent(android.app.Activity contextActivity,
AuthenticationRequest request)
Get an intent to open the LoginActivity.
|
static AuthenticationResponse |
getResponse(int resultCode,
android.content.Intent intent)
Extracts
AuthenticationResponse
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,
String campaign)
Opens Spotify in the Play Store or browser.
|
static void |
openLoginActivity(android.app.Activity contextActivity,
int requestCode,
AuthenticationRequest request)
Opens LoginActivity which performs authorization.
|
static void |
openLoginInBrowser(android.app.Activity contextActivity,
AuthenticationRequest 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
|
public static void openLoginInBrowser(android.app.Activity contextActivity, AuthenticationRequest request)
contextActivity
- The activity that should start the intent to open a browser.request
- Authentication requestpublic static android.content.Intent createLoginActivityIntent(android.app.Activity contextActivity, AuthenticationRequest request)
// To start LoginActivity from a Fragment:
Intent intent = AuthenticationClient.createLoginActivityIntent(getActivity(), request);
startActivityForResult(intent, REQUEST_CODE);
// To close LoginActivity
AuthenticationClient.stopLoginActivity(getActivity(), REQUEST_CODE);
contextActivity
- A context activity for the LoginActivity.request
- Authentication requestIllegalArgumentException
- if any of the arguments is nullpublic static void openLoginActivity(android.app.Activity contextActivity, int requestCode, AuthenticationRequest request)
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.contextActivity
- A context activity for the LoginActivity.requestCode
- Request code for LoginActivity.request
- Authentication requestIllegalArgumentException
- if any of the arguments is nullpublic static void stopLoginActivity(android.app.Activity contextActivity, int requestCode)
contextActivity
- The activity that was used to launch LoginActivity
with openLoginActivity(android.app.Activity, int, AuthenticationRequest)
requestCode
- Request code that was used to launch LoginActivitypublic static AuthenticationResponse getResponse(int resultCode, android.content.Intent intent)
AuthenticationResponse
from the LoginActivity result.resultCode
- Result code returned with the activity result.intent
- Intent received with activity result. Should contain a Uri with result data.public static void openDownloadSpotifyActivity(android.app.Activity contextActivity)
contextActivity
- The activity that should start the intent to open the download page.public static void openDownloadSpotifyActivity(android.app.Activity contextActivity, String campaign)
contextActivity
- The activity that should start the intent to open the download page.campaign
- A Spotify-provided campaign ID. null
if not provided.public static boolean isAvailable(android.content.Context ctx, android.content.Intent intent)