Skip to main content

Unity

Requirements

  • Snap Kit for Unity SDK
  • Client ID from the developer portal
  • Unity 2019.4
  • Android API Level 21+

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

You can download our official Snap Kit Unity SDK from the Unity Asset Store. Once downloaded, import the package into your project.

  1. Download the latest release of Snapkit.unitypackage.
  2. With your Unity project open, double click the package file and import all files.
  3. After import is done, find the Assets/Snap/Editor/SnapKitSettings.asset file folder in your project explorer.
  4. (Optional) If the file is not present, create a new one in that folder by selecting Assets->Create->Snap Kit Settings.
  5. Click on the SnapKitSettings asset and make sure that the Inspector pane is visible.
  6. In the Inspector pane, populate the information from your Snap Kit Portal registered app:
    • Client ID: your "OAuth2 Client ID".
    • Redirect URI: one of your "Redirect URIs for OAuth".
    • URL Scheme, Host and Prefix: these are parts of the chosen Redirect URI. For example, if the URI is myapp://snap-kit/oauth/, the URL Scheme is myapp, the Host is snap-kit, and the Path Prefix is /oauth.
  7. Go to Player Settings and make sure that the package name chosen for your app is one of the pre-defined "Platform Identifiers" defined on the Snap Kit portal.

Initiate Login with Snapchat

The example below shows a minimal MonoBehaviour that can perform Login with Snapchat:

using Snap;
using UnityEngine;

public class LoginManager : MonoBehaviour
{
void OnEnable()
{
LoginKit.OnLoginLinkDidSucceedEvent += OnLoginSuccess;
LoginKit.OnLoginLinkDidFailEvent += OnLoginFail;
}

void OnDisable()
{
LoginKit.OnLoginLinkDidSucceedEvent -= OnLoginSuccess;
LoginKit.OnLoginLinkDidFailEvent -= OnLoginFail;
}

public void OnButtonTapped_Login()
{
LoginKit.Login();
}

void OnLoginSuccess()
{
Debug.Log("Login Succeeded");
}

void OnLoginFail()
{
Debug.Log("Login Failed");
}
}

Query Login State

To check whether the user is currently logged in, use LoginKit.IsLoggedIn:

bool isUserLoggedIn = LoginKit.IsLoggedIn();

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. The following example makes a request for both using LoginKit.FetchUserDataWithQuery:

void FetchUserData()
{
// Send the query
String query = "{me{displayName, bitmoji{avatar}}}";
LoginKit.FetchUserDataWithQuery(query, null);
}

void OnFetchUserDataSucceededEvent(string json)
{
// Success!
Debug.Log("Fetch user data succeeded.")
}

void OnFetchUserDataFailedEvent(string errorOrNull)
{
Debug.Log("Fetch user data failed. Check your app settings <br/> " + errorOrNull);
}

void OnEnable()
{
// Subscribe to event updates
LoginKit.OnFetchUserDataSucceededEvent += OnFetchUserDataSucceededEvent;
LoginKit.OnFetchUserDataFailedEvent += OnFetchUserDataFailedEvent;
}

void OnDisable()
{
// Unsubscribe to event updates
LoginKit.OnFetchUserDataSucceededEvent -= OnFetchUserDataSucceededEvent;
LoginKit.OnFetchUserDataFailedEvent -= OnFetchUserDataFailedEvent;
}

Send Requests to Get the 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. The following example requests this identifier for the user that is currently logged in and saves it for use in other Snap Kit features. This can also be achieved through LoginKit.FetchUserDataWithQuery:

void FetchUserData()
{
// Send the query
String query = "{me{externalID}}";
LoginKit.FetchUserDataWithQuery(query, null);
}

void OnFetchUserDataSucceededEvent(string json)
{
// Success!
Debug.Log("Fetch user data succeeded.")
}

void OnFetchUserDataFailedEvent(string errorOrNull)
{
Debug.Log("Fetch user data failed. Check your app settings <br/> " + errorOrNull);
}

void OnEnable()
{
// Subscribe to event updates
LoginKit.OnFetchUserDataSucceededEvent += OnFetchUserDataSucceededEvent;
LoginKit.OnFetchUserDataFailedEvent += OnFetchUserDataFailedEvent;
}

void OnDisable()
{
// Unsubscribe to event updates
LoginKit.OnFetchUserDataSucceededEvent -= OnFetchUserDataSucceededEvent;
LoginKit.OnFetchUserDataFailedEvent -= OnFetchUserDataFailedEvent;
}

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 LoginKit.UnlinkAllSessions() method below can be used to clear the access locally.

LoginKit.UnlinkAllSessions();

Once this command is executed, FetchUserDataWithQuery() requests will result in permission errors.

What’s Next

Was this page helpful?
Yes
No

AI-Powered Search