Login Kit

Login Kit Android gives you access to two Snapchat features, login and identity. Logging in via Snapchat removes the obstacles of signup and login for new users. The display name and Bitmoji avatar give users a familiar identity within your app.

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+

Understanding 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>

Getting started

We share the Login Kit and Core Kit AARs from our Maven repository. To access them, add the following code block to your root project's build.gradle file:

repositories {
   maven {
       url "https://storage.googleapis.com/snap-kit-build/maven"
   }
}

Note: If you have trouble accessing Google (used in the link above), you can use our GitHub Maven repository with the following code block:

repositories {
   maven {
       url "https://raw.githubusercontent.com/Snap-Kit/release-maven/repo"
   }
}

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

dependencies {
   ...
   implementation([
           'com.snapchat.kit.sdk:login:1.1.4',
           'com.snapchat.kit.sdk:core:1.1.4'
   ])
}

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

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

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

   ...

   <activity
       android:name="com.snapchat.kit.sdk.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>

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>
    </string-array>
</resources>

Initiating login with Snapchat

The SDK provides a button that initiates the Snapchat login process. A tap on this button in your app redirects users to Snapchat if they have it installed. If not, it prompts them to log in with Snapchat through an in-app web view.

To add the login 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.snapchat.kit.sdk.SnapLogin;

View mLoginButton = SnapLogin.getButton(getContext(), (ViewGroup)mViewRoot);

To add the login flow using a custom button, it’s a little different:

View yourView = findViewById(...);
yourView.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick() {
        SnapLogin.getAuthTokenManager(this).startTokenGrant();
   }
});

Now when a user taps the button, your app will start auth with Snapchat!


Subscribing to login state updates

For updates on the success of this login process, define an onLoginStateChangedListener:

// Import needed for LoginStateController
import com.snapchat.kit.sdk.core.controller.LoginStateController;

final LoginStateController.OnLoginStateChangedListener mLoginStateChangedListener =
    new LoginStateController.OnLoginStateChangedListener() {
        @Override
        public void onLoginSucceeded() {
            // Here you could update UI to show login success
        }

        @Override
        public void onLoginFailed() {
            // Here you could update UI to show login failure
        }

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

// Add the LoginStateChangedListener you’ve defined to receive LoginInState updates
SnapLogin.getLoginStateController(getContext()).addOnLoginStateListener(mLoginStateChangedListener);

```java

To unsubscribe your `LoginStateChangedListener` from updates, use the following line:

```java
SnapLogin.getLoginStateController(this).removeOnLoginStateChangedListener(mLoginStateChangedListener);

Querying login state

To check whether the user is currently logged in, use SnapLogin.isUserLoggedIn:

boolean isUserLoggedIn = SnapLogin.isUserLoggedIn(getContext());

Sending 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. The following example makes a request for both. Upon success, this example callback loads the display name into a TextView mDisplayNameTextView and loads the Bitmoji into an ImageView mBitmojiImageView:

String query = "{me{bitmoji{avatar},displayName}}";
String variables = null;
SnapLogin.fetchUserData(this, query, variables, new FetchUserDataCallback() {
    @Override
    public void onSuccess(@Nullable UserDataResponse userDataResponse) {
        if (userDataResponse == null || userDataResponse.getData() == null) {
            return;
        }

        MeData meData = userDataResponse.getData().getMe();
        if (meData == null) {
            return;
        }

        mNameTextView.setText(userDataResponse.getData().getMe().getDisplayName());

        if (meData.getBitmojiData() != null) {
            Glide.with(getContext())
                    .load(meData.getBitmojiData().getAvatar())
                    .into(mBitmojiImageView);
        }
    }

    @Override
    public void onFailure(boolean isNetworkError, int statusCode) {

    }
});

Sending requests to get the external ID

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. The following example requests this identifier for the user that is currently logged in and saves it for use in other Snap Kit features:

String query = "{me{externalId}}";
SnapLogin.fetchUserData(this, query, null, new FetchUserDataCallback() {
    @Override
    public void onSuccess(@Nullable UserDataResponse userDataResponse) {
        if (userDataResponse == null || userDataResponse.getData() == null) {
            return;
        }

        MeData meData = userDataResponse.getData().getMe();
        if (meData == null) {
            return;
        }

        mBackend.save(userDataResponse.getData().getMe().getExternalId());
    }

    @Override
    public void onFailure(boolean isNetworkError, int statusCode) {

    }
});

Unlinking

A user can choose to end the current OAuth2 Snapchat session and no longer share their display name and Bitmoji avatar with your app. The app will also be removed from the Connected Apps page in Snapchat. However, if the user has multiple ongoing sessions, another session may still work. To kill the current OAuth2 session, use:

SnapLogin.getAuthTokenManager(this).revokeToken();

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

What’s next

PreviousLogin Kit iOS
NextLogin Kit Web
Is this page helpful?
Thanks for your feedback!