Beta
In this tutorial, we explain how to use Spotify's Android auth-lib
. The Android auth-lib
is a small library included in the Android Spotify SDK, which authenticates the user and allows apps to get an authorization code through the Spotify client. The authorization code can be exchanged for an access token as explained here. The access token can then be used with Spotify's API.
The auth-lib
is independent of the app-remote
library, which is also included in the Android Spotify SDK.
In Android Studio, edit the build.gradle
file in the app directory (it can also be labelled as Module: app
) and make sure it contains the dependency on the library. Check Maven Central Repository to make sure you're using the latest library version in your project.
To be able to get the library dependency, you should also add mavenCentral()
into a repositories block. Your updated Gradle file should look something like this:
_11
implementation 'com.spotify.android:auth:1.2.5' // Maven dependency
_11
// All other dependencies for your app should also be here:
_11
implementation 'androidx.browser:browser:1.0.0'
_11
implementation "androidx.appcompat:appcompat:$appCompatVersion"
There are two basic ways you can authorize your application to get access to the data served by Spotify APIs:
Using the Spotify client to authenticate the userThis is the preferred and recommended method of authentication because users don't need to enter their credentials.
If Spotify is installed on the device, the auth-lib
will connect to the Spotify client and fetch the authorization code for the current user. Since the user is already logged into Spotify, they don’t need to type in their username and password. If the application requests scopes that have not been approved before, the user will see a list of scopes and will need to accept them.
If Spotify is not installed on the device, the auth-lib will fallback to the WebView
based authorization and open the Spotify Accounts login page at https://accounts.spotify.com in a native WebView
. User will have to enter their username and password to login to Spotify and accept the supplied scopes.
In both cases the result of the authorization flow will be returned in the onActivityResult
method of the activity that initiated it.
This flow is completed within the application; there is no need to open a web browser.
Logging InTo login using this flow, open the LoginActivity
from one of your activities:
_11
// Request code will be used to verify if result comes from the login activity. Can be set to any integer.
_11
private static final int REQUEST_CODE = 1337;
_11
private static final String REDIRECT_URI = "yourcustomprotocol://callback";
_11
AuthorizationRequest.Builder builder =
_11
new AuthorizationRequest.Builder(CLIENT_ID, AuthorizationResponse.Type.CODE, REDIRECT_URI);
_11
builder.setScopes(new String[]{"streaming"});
_11
AuthorizationRequest request = builder.build();
_11
AuthorizationClient.openLoginActivity(this, REQUEST_CODE, request);
To receive the authorization result, your activity needs to override the onActivityResult
callback:
_24
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
_24
super.onActivityResult(requestCode, resultCode, intent);
_24
// Check if result comes from the correct activity
_24
if (requestCode == REQUEST_CODE) {
_24
AuthorizationResponse response = AuthorizationClient.getResponse(resultCode, intent);
_24
switch (response.getType()) {
_24
// Response was successful and contains auth token
_24
// Handle successful response
_24
// Auth flow returned an error
_24
// Handle error response
_24
// Most likely auth flow was cancelled
_24
// Handle other cases
Logging Out
By default, the authenticated session is persisted in the WebView
, which allows user to log in again without re-typing in their password.
To log out and clear all stored tokens, use the AuthorizationClient#clearCookies
method. Both Spotify and Facebook tokens will be removed.
Only if the first way is not possible.
This method opens the Spotify Accounts page in a separate, external browser window, completes the authentication process, and then redirects back to your application. This method does not involve the auth-lib.
Logging inTo log in using the web browser, open it from one of your activities:
_10
private static final String REDIRECT_URI = "yourcustomprotocol://callback";
_10
AuthorizationRequest.Builder builder =
_10
new AuthorizationRequest.Builder(CLIENT_ID, AuthorizationResponse.Type.CODE, REDIRECT_URI);
_10
builder.setScopes(new String[]{"streaming"});
_10
AuthorizationRequest request = builder.build();
_10
AuthorizationClient.openLoginInBrowser(this, request);
The activity that will receive and process the result must be configured in the AndroidManifest.xml
:
_23
android:name=".MySpotifyAuthorizationActivity"
_23
android:label="@string/app_name"
_23
android:launchMode="singleInstance" >
_23
// An intent filter that will receive the response
_23
// from the authentication service
_23
<action android:name="android.intent.action.VIEW"/>
_23
<category android:name="android.intent.category.DEFAULT"/>
_23
<category android:name="android.intent.category.BROWSABLE"/>
_23
// this needs to match the scheme and host of the redirect URI as defined in My applications page
_23
android:host="callback"
_23
android:scheme="yourcustomprotocol"/>
_23
// Other intent filters this activity requires
To process the result, the receiving activity (MySpotifyAuthorizationActivity
in this example) needs to override one of its callbacks. With launch mode set to singleInstance
or singleTask
the callback to use is onNewIntent
:
_22
protected void onNewIntent(Intent intent) {
_22
super.onNewIntent(intent);
_22
Uri uri = intent.getData();
_22
AuthorizationResponse response = AuthorizationResponse.fromUri(uri);
_22
switch (response.getType()) {
_22
// Response was successful and contains auth token
_22
// Handle successful response
_22
// Auth flow returned an error
_22
// Handle error response
_22
// Most likely auth flow was cancelled
_22
// Handle other cases
It is also possible to use other launch modes for the activity that processes the authorization result. In this case it can be either onNewIntent
or onCreate
callback that will receive an intent containing the result. See information about launch modes in Android to choose the correct mode.
Be aware of the fact that activities launched in standard
or singleTop
mode can have multiple instances existing at the same time. Make sure you don’t create multiple Player
instances in your application.
To log out user from Spotify in the app, they must be logged out using the same browser they used to log in. To log out, open url https://accounts.spotify.com in the browser. The user that is currently logged in will then be able to log out:
Another option to log out is to add showDialog
parameter to the authorization request. This will force the page that lists the granted scopes and currently logged-in user giving them the chance to log out by choosing the “Not you?” link:
_10
private static final String REDIRECT_URI = "yourcustomprotocol://callback";
_10
AuthorizationRequest.Builder builder =
_10
new AuthorizationRequest.Builder(CLIENT_ID, AuthorizationResponse.Type.CODE, REDIRECT_URI);
_10
builder.setScopes(new String[]{"streaming"});
_10
builder.setShowDialog(true);
_10
AuthorizationRequest request = builder.build();
_10
AuthorizationClient.openLoginInBrowser(this, request);
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4