Usage
To obtain tokens, follow these steps:
Step 1. Initialization
-
Add this code to your app's
build.gradle
file:android { defaultConfig { manifestPlaceholders = [YANDEX_CLIENT_ID:"<app ID_clientID>"] } }
- You can find the app's Client ID in its properties.
-
Use the following code for initialization:
KotlinJavaval sdk = YandexAuthSdk.create(YandexAuthOptions(requireContext()))
YandexAuthSdk sdk = YandexAuthSdk.create(new YandexAuthOptions(requireContext()));
Step 2. Authorization
-
Start the authorization:
KotlinJavaval sdk = YandexAuthSdk.create(YandexAuthOptions(requireContext())) val launcher = registerForActivityResult(sdk.contract) { result -> handleResult(result) } val loginOptions = YandexAuthLoginOptions() launcher.launch(loginOptions)
YandexAuthSdk sdk = YandexAuthSdk.create(new YandexAuthOptions(requireContext())); ActivityResultLauncher<YandexAuthLoginOptions> launcher = registerForActivityResult(sdk.getContract(), result -> handleResult(result)); YandexAuthLoginOptions loginOptions = new YandexAuthLoginOptions(); launcher.launch(loginOptions);
-
To handle authorization events, use code like this:
KotlinJavaprivate fun handleResult(result: YandexAuthResult) { when (result) { is YandexAuthResult.Success -> onSuccessAuth(result.token) is YandexAuthResult.Failure -> onProccessError(result.exception) YandexAuthResult.Cancelled -> onCancelled() } }
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(); } }
Step 3. Obtaining a JSON Web Token
To obtain a JSON Web Token, use the following method:
Kotlin
Java
sdk.getJwt(yandexAuthToken)
sdk.getJwt(yandexAuthToken);
Step 4. Getting user information
You can exchange the obtained token for user information.
Useful resources
Was the article helpful?
Previous
Next