Skip to main content

Android

Requirements

  • Client ID from the developer portal
  • Android Studio 3.0+
  • Gradle 3.0+
  • Android API Level 19+

To connect your app to Snapchat, your app must also have the following targets:

  • Android support library version 22+
  • Snapchat 10.34.0.0+

Understand Scopes

Scopes let your application declare which Login Kit features it wants access to. If a scope is toggleable, the user can deny access to one scope while agreeing to grant access to others.

Login Kit offers the following scopes:

  • https://auth.snapchat.com/oauth2/api/user.display_name: Grants access to the user's Snapchat display name
  • https://auth.snapchat.com/oauth2/api/user.bitmoji.avatar: Grants access to the user's Bitmoji avatar; toggleable by user

When you specify which scopes you'd like, use the full URL, like this:

  <key>SCSDKScopes</key>
<array>
<string>https://auth.snapchat.com/oauth2/api/user.bitmoji.avatar</string>
<!-- other scopes you might have... -->
</array>

Get Started

We share the Login Kit AAR from Maven Central Repository. To access them, add the following code block:

root-level build.gradle (if using com.android.tools.build:gradle < 7.0)
allprojects {
repositories {
google()
mavenCentral()
}
}
settings.gradle (if using com.android.tools.build:gradle >= 7.0)
dependencyResolutionManagement {
. . .
repositories {
google()
mavenCentral()
}
}
. . .

Next, add the following implementation to the dependencies section of your application’s build.gradle file, not the root project’s build.gradle:

app-level build.gradle
dependencies {
...
implementation([
'com.snap.loginkit:loginkit:2.1.0',
])
}

Finally, add your application’s client ID, redirect URL, and scopes to the appropriate meta-data tags in your application’s AndroidManifest.xml. The SDK automatically fetches these values to enable deeplinking from your app to Snapchat. For a list of available scopes, please see the Understanding Scopes section of this document

AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />

<application ...>
<meta-data android:name="com.snap.kit.clientId" android:value="your app’s client id" />
<meta-data android:name="com.snap.kit.redirectUrl" android:value="the url that will handle login completion" />
<meta-data android:name="com.snap.kit.scopes" android:resource="@array/snap_connect_scopes" /> <!-- This should be a string array of scopes !-->

...

<activity
android:name="com.snap.corekit.SnapKitActivity"
android:launchMode="singleTask"
>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!--
Enter the parts of your redirect url below
e.g., if your redirect url is myapp://snap-kit/oauth2
android:scheme="myapp"
android:host="snap-kit"
android:path="oauth2"
!-->
<data
android:scheme="the scheme of your redirect url"
android:host="the host of your redirect url"
android:path="the path of your redirect url"
/>
</intent-filter>

</activity>

...
</application>

If your app targets Android 11 (API level 30) or higher, you will need to include the following package query in your app's AndroidManifest.xml:

AndroidManifest.xml
<queries>
<package android:name="com.snapchat.android" />
</queries>

In the example above, you'll have to define snap_connect_scopes as an Android resource array in values/arrays.xml.

Example:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="snap_connect_scopes">
<item>https://auth.snapchat.com/oauth2/api/user.bitmoji.avatar</item>
<item>https://auth.snapchat.com/oauth2/api/user.display_name</item>
</string-array>
</resources>

As of v1.4.4, Snap Kit is supported on x86 automatically. To filter out the x86 libraries, include the following snippet in your app's build.gradle:

build.gradle
defaultConfig {
ndk {
abiFilters 'armeabi-v7a', 'arm64-v8a'
}
}

For versions older than v2.1.0, you can add support for Snap Kit on x86 and x86_64, by including the following snippet in your app's build.gradle:

build.gradle
configurations.all {
resolutionStrategy {
force 'com.snapchat.kit.sdk:core:' + <versionName> + ' x86'

}
} // Where <versionName> is the Snap Kit version you are using (Minimum 1.2.0)

The Snap Kit SDK is initialized on application start-up by default. Auto-initialization can be disabled and the SDK can be manually initialized and de-initialized.

To disable Snap Kit SDK auto-initialization in your application, add the following provider section to your app’s AndroidManifest.xml file between the application tags as shown:

AndroidManifest.xml
<application>
...
<provider
android:name="com.snap.corekit.SnapKitInitProvider"
android:enabled="false"
/>
</application>

To manually initialize the Snap Kit SDK, use the SnapKit#initSDK(Context context) method.

To de-initialize the Snap Kit SDK, use the SnapKit#deinitialize() method.

Features

Initiate Login with Snapchat

The SDK provides a button that initiates the Snapchat login process. When users tap on this button in your app, they are redirected to Snapchat if they have it installed. If they do not have Snapchat installed, it prompts them to log in with Snapchat through an in-app web view. You can either add a Snapchat button or a custom button to your app.

To add a Snapchat button to your app, add the import and view below. This adds the button to your desired root view with the default styling.

import com.snap.loginkit.SnapLogin;
import com.snap.loginkit.SnapLoginProvider;

// Get the login API
SnapLogin snapLogin = SnapLoginProvider.get(getContext());

// Attach the Snapchat login button to your view root
View loginButton = snapLogin.getButton((ViewGroup) viewRoot);

To add a custom button to your app for the login flow:

import com.snap.loginkit.LoginResultCallback;
import com.snap.loginkit.SnapLogin;
import com.snap.loginkit.SnapLoginProvider;
import com.snap.loginkit.exceptions.LoginException;

// Get the login API
SnapLogin snapLogin = SnapLoginProvider.get(getContext());

View yourButtonView = findViewById(...);
yourButtonView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Start token grant
snapLogin.startTokenGrant(new LoginResultCallback() {
@Override
public void onStart() {
// Here you could update the UI to show login start
}

@Override
public void onSuccess(@NonNull String accessToken) {
// Here you could update the UI to show login success
}

@Override
public void onFailure(@NonNull LoginException exception) {
// Here you could update the UI to show login failure
}
});

}
});

Now when a user taps the button, your app will start authentication with Snapchat.

Subscribe / Unsubscribe to Login State Updates

To subscribe to updates about the success of the login process, use addLoginStateCallback(). To unsubscribe from login updates, use removeLoginStateCallback().

import com.snap.loginkit.LoginStateCallback;
import com.snap.loginkit.SnapLogin;
import com.snap.loginkit.SnapLoginProvider;
import com.snap.loginkit.exceptions.LoginException;

// Implement the callback interface
private static LoginStateCallback loginStateCallback = new LoginStateCallback() {

@Override
public void onStart() {
// Here you could update the UI to show login start
}

@Override
public void onSuccess(@NonNull String accessToken) {
// Here you could update the UI to show login success
}

@Override
public void onFailure(@NonNull LoginException exception) {
// Here you could update the UI to show login failure
}

@Override
public void onLogout() {
// Here you could update the UI to reflect the logged out state
}
};

. . .

// Get the login API
SnapLogin snapLogin = SnapLoginProvider.get(getContext());

// Subscribe for login updates
snapLogin.addLoginStateCallback(loginStateCallback);

// Unsubscribe from login updates
snapLogin.removeLoginStateCallback(loginStateCallback);

Query Login State

To check whether a user is currently logged in, use snapLogin.isUserLoggedIn.

// Get the login API
SnapLogin snapLogin = SnapLoginProvider.get(getContext());

// Query user’s logged-in state
snapLogin.isUserLoggedIn();

Send Requests to Get User Data

Once a user logs into your app with Snapchat, you can make requests for their display name or Bitmoji avatar.

In the following example, requests are made to get a user's display name and Bitmoji avatar. Upon success, the callback loads the display name into a nameTextView and loads the Bitmoji into a bitmojiImageView.

import com.snap.loginkit.BitmojiQuery;
import com.snap.loginkit.SnapLogin;
import com.snap.loginkit.SnapLoginProvider;
import com.snap.loginkit.UserDataQuery;
import com.snap.loginkit.UserDataResultCallback;
import com.snap.loginkit.exceptions.UserDataException;
import com.snap.loginkit.models.MeData;
import com.snap.loginkit.models.UserDataResult;

private void fetchUserData(final Context context) {
// Construct the bitmoji query
final BitmojiQuery bitmojiQuery = BitmojiQuery.newBuilder()
// optional: for 'avatarID' resource
.withAvatarId()
// optional: for 'twoDAvatarUrl' resource
.withTwoDAvatarUrl()
.build();

// Construct the user data query
final UserDataQuery userDataQuery = UserDataQuery.newBuilder()
// optional: for 'displayName' resource
.withDisplayName()
// optional: for 'externalID' resource
.withExternalId()
// optional: for Snap OIDC (OpenID Connect) token
// Snap OIDC (OpenID Connect) provides a generic authentication and identity solution
// that allows otherwise different systems to interoperate and share authentication state
// and user profile information.
// Typically, this allows 3rd party backend services to accept and authenticate requests
// from Snap clients.
.withIdToken()
// optional: for ‘bitmoji’ resource
.withBitmoji(bitmojiQuery)
.build();


// Get the login API
SnapLogin snapLogin = SnapLoginProvider.get(context);

// Call the fetch API
snapLogin.fetchUserData(userDataQuery, new UserDataResultCallback() {
@Override
public void onSuccess(@NonNull UserDataResult userDataResult) {
// Handle Success
if (userDataResult.getData() == null) {
return;
}

MeData meData = userDataResult.getData().getMeData();
if (meData == null) {
return;
}

nameTextView.setText(meData.getDisplayName());

if (meData.getBitmojiData() != null
&& meData.getBitmojiData().getTwoDAvatarUrl() != null) {
Glide.with(context)
.load(meData.getBitmojiData().getTwoDAvatarUrl())
.into(bitmojiImageView);
}

}

@Override
public void onFailure(@NonNull UserDataException exception) {
// Handle Failure
}
});
}

Send Requests to Get externalId

Once a user logs into your app with Snapchat, you can make requests for their external ID. This is a unique identifier for this user on your app.

In the following example, a request are made to get the externalId for a user who is currently logged in. This ID is then saved for use in other Snap Kit features.

import com.snap.loginkit.SnapLogin;
import com.snap.loginkit.SnapLoginProvider;
import com.snap.loginkit.UserDataQuery;
import com.snap.loginkit.UserDataResultCallback;
import com.snap.loginkit.exceptions.UserDataException;
import com.snap.loginkit.models.MeData;
import com.snap.loginkit.models.UserDataResult;

private void fetchUserData(final Context context) {
// Construct the user data query
final UserDataQuery userDataQuery = UserDataQuery.newBuilder()
// optional: for 'externalID' resource
.withExternalId()
.build();

// Get the login API
SnapLogin snapLogin = SnapLoginProvider.get(context);

// Call the fetch API
snapLogin.fetchUserData(userDataQuery, new UserDataResultCallback() {
@Override
public void onSuccess(@NonNull UserDataResult userDataResult) {
// Handle Success
if (userDataResult.getData() == null) {
return;
}

MeData meData = userDataResult.getData().getMeData();
if (meData == null) {
return;
}

myBackend.save(meData.getExternalId());
}

@Override
public void onFailure(@NonNull UserDataException exception) {
// Handle Failure
}
});

}

A user can choose to end the current OAuth2 Snapchat session and stop sharing their Display Name and Bitmoji avatar with your app. The clearToken() method below can be used to clear the access and refresh tokens locally.

import com.snap.loginkit.SnapLogin;
import com.snap.loginkit.SnapLoginProvider;

// Get the login API
SnapLogin snapLogin = SnapLoginProvider.get(getContext());

// Clear the access and refresh token locally
snapLogin.clearToken();

Once the clearToken() command is executed, fetchUserData() requests will result in permission errors.

Check a User's Stated Age

Stated Age Check is a mandatory feature of the Login Kit SDK that enforces minimum age restrictions for certain Snap Kit applications and will prevent users who are under the age of 18 from establishing Login Kit connections.

This feature is included in the existing Login Kit integration and does not require additional work from developers. If your app requires Stated Age Check, users will see a non-toggleable "Check your Stated Age" field when connecting to your application.

Due to Snap's Privacy Policy, all apps that are categorized as friend finding or matching will have Stated Age Check toggled during the review state.

Birthday Updates

If your app requires Stated Age Check and a user updates their birthdate in Snapchat after going through the Login Kit connection flow, Snap will disconnect their connection to your app.

Web Login Kit will not work for apps that require Stated Age Check.

Sample Application

Please visit our public GitHub for a sample application using Login Kit on Android.

What’s Next

Was this page helpful?
Yes
No

AI-Powered Search