---
metadata:
  - name: generator
    content: Diplodoc Platform v5.44.0
alternate:
  - https://yandex.com/dev/id/doc/en/mobileauthsdk/android/archive/3.1.0/sdk-android-use.md
  - https://yandex.com/dev/id/doc/ru/mobileauthsdk/android/archive/3.1.0/sdk-android-use.md
---
> **Documentation Index:** Fetch the complete configuration index at https://yandex.com/dev/id/doc/en/llms.txt

# Using the SDK

To obtain tokens, follow these steps:

## Step 1. Initialization {#initialisation}

1. Add this code to your app's `build.gradle` file:

   ```java
   android {
         defaultConfig {
         manifestPlaceholders = [YANDEX_CLIENT_ID:"<app ID_clientID>"]
      }
   }
   ```

   * You can find the app's **Client ID** in [its properties](https://yandex.com/dev/id/doc/en/register-client.md#app-params).

1. Use the following code for initialization:

   {% list tabs group=kotlin-java %}

   - Kotlin

      ```java
      val sdk = YandexAuthSdk.create(YandexAuthOptions(requireContext()))
      ```

   - Java

      ```java
      YandexAuthSdk sdk = YandexAuthSdk.create(new YandexAuthOptions(requireContext()));
      ```

   {% endlist %}


## Step 2. Authorization {#authorization}

1. Start the authorization:

   {% list tabs group=kotlin-java %}

   - Kotlin

      ```java
      val sdk = YandexAuthSdk.create(YandexAuthOptions(requireContext()))
      val launcher = registerForActivityResult(sdk.contract){ result -> handleResult(result) }
      val loginOptions = YandexAuthLoginOptions()
      launcher.launch(loginOptions)
      ```

   - Java

      ```java
      YandexAuthSdk sdk = YandexAuthSdk.create(new YandexAuthOptions(requireContext()));
      ActivityResultLauncher<YandexAuthLoginOptions> launcher =
         registerForActivityResult(sdk.getContract(), result -> handleResult(result));
      YandexAuthLoginOptions loginOptions = new YandexAuthLoginOptions();
      launcher.launch(loginOptions);
      ```

   {% endlist %}

2. To handle authorization events, use code like this:

   {% list tabs group=kotlin-java %}

   - Kotlin

      ```java
      private fun handleResult(result: YandexAuthResult) {
         when (result){
            is YandexAuthResult.Success -> onSuccessAuth(result.token)
            is YandexAuthResult.Failure -> onProccessError(result.exception)
            YandexAuthResult.Cancelled -> onCancelled()
         }
      }
      ```

   - Java

      ```java
      private void handleResult(YandexAuthResult result) {
         if (result instanceof YandexAuthResult.Success){
            onSuccessAuth(((YandexAuthResult.Success) result).getToken());
         } else if (result instanceof YandexAuthResult.Failure){
            onProccessError(((YandexAuthResult.Failure) result).getException());
         } else{
            onCancelled();
         }
      }
      ```

   {% endlist %}

## Step 3. Obtaining a JSON Web Token {#jwt}

To obtain a [JSON Web Token](https://yandex.com/dev/id/doc/en/tokens/jwt.md), use the following method:

{% list tabs group=kotlin-java %}

- Kotlin

   ```java
   sdk.getJwt(yandexAuthToken)
   ```

- Java

   ```java
   sdk.getJwt(yandexAuthToken);
   ```

{% endlist %}


## Step 4. Getting information about the user {#user-info}

You can exchange the obtained token for [user information](https://yandex.com/dev/id/doc/en/user-information.md).

## Useful links {#useful-links}

* [Exchanging the token for information about the user](https://yandex.com/dev/id/doc/en/user-information.md)
* [Function guide](https://yandex.com/dev/id/doc/en/mobileauthsdk/android/archive/3.1.0/sdk-android-methods.md)
* [Error codes](https://yandex.com/dev/id/doc/en/mobileauthsdk/android/archive/3.1.0/sdk-android-errors.md)
