Mobile applications

Getting a token for a mobile application:

  1. The application directs the user to the Yandex.OAuth page, where the user can grant access to personal data. The page can be opened in a browser or in the WebView element embedded in the application.

  2. The user grants the application access.

  3. Yandex.OAuth redirects the user to the address specified in the Callback URL field during application registration. Token data is passed in the redirect URL after the # symbol.

    To intercept the URL from the browser, you can use a custom URI scheme in the address, such as myapp://token. Going to this type of URL will launch the application that is registered in the operating system as the handler for the myapp scheme.

  4. The application gets the redirect address and extracts the token.

Tip. Debugging tokens can be obtained manually.

The received token can be saved in the application and used for API requests until its lifespan expires. The token must only be accessible to your application, so we do not recommend storing it in browser cookies, open configuration files, and so on.

Token request URL

The application should direct the user to Yandex.OAuth at the following address:

https://oauth.yandex.com/authorize?
   response_type=token
 & client_id=<application ID>
[& device_id=<device ID>]
[& device_name=<device name>]
[& display=popup]
[& login_hint=<username or email>]
[& scope=<necessary permissions requested>]
[& optional_scope=<optional permissions requested>]
[& force_confirm=yes]
[& state=<any string>]
ParameterDescription
Required parameters

response_type

Required response.

When requesting a token, specify the value “token”.

client_id

Application ID. Available in the application properties (click the name of the application to open its properties).

Additional parameters
device_id

Unique ID of the device that the token is being requested for. To ensure uniqueness, it is sufficient to generate the UUID once and use it for every request for a new token from this device.

The ID must be at least 6 characters and no more than 50. Only printable ASCII symbols are allowed (codes 32 through 126).

Restriction. An application can't have more than 20 tokens associated with devices for a specific user. If Yandex.OAuth successfully issues the application a new token for a device, the oldest token stops working.

For more information about device tokens, see Device token.

device_name

The device name to show to users. Maximum of 100 characters.

For mobile devices, we recommend passing the device name set by the user. If it does not have a name, you can make one up using the device model, OS name and version, and so on.

If the device_name parameter was passed without the device_id parameter, it will be ignored. Yandex.OAuth can only issue a regular token that is not associated with the device.

If the device_id parameter was passed without the device_name parameter, the user interface will show the token as issued for an unknown device.

display

Indicates the lightweight layout (without standard Yandex navigation) for the page for granting access.

The lightweight layout should be requested if, for example, the page for granting access must be displayed in a small popup window.

Only the “popup” value is processed. Other values are ignored.

login_hint

Explicitly identifies the account that the token is being requested for. The parameter value can pass a Yandex account username, or an email address for Yandex.Mail or Yandex.Mail for Domain.

This parameter helps the user log in on Yandex with the account that the application needs to access. After getting the parameter, Yandex.OAuth checks the user's authorization:

  • If the user is already logged in under the necessary account, Yandex.OAuth just asks for permission to access data.

  • If the user isn't logged in under the necessary account, the service displays the Yandex login page with this parameter value shown in the username field. Keep in mind that the token won't necessarily be requested for the account shown: the user might delete the pre-filled username and log in with another one.

If the parameter specifies a non-existent account, Yandex.OAuth can only inform the user of this. The application will have to request the token over again.

scope

A space-separated list of permissions that the application needs at this time. The requested permissions must be from the list that was specified when registering the application. To find out which permissions are available, go to https://oauth.yandex.com/client/<client_id>/info (specify the application's ID in place of <client_id>).

If the scope and optional_scope parameters were omitted from the request, the token is issued with the permissions that were specified when registering the application.

This parameter lets you get a token for just the permissions that the application needs right now.

Note. Access permissions that are requested using both the scope parameter and the optional_scope parameter simultaneously are treated as optional permissions that the application can do without. The user can decide which of the requested permissions to grant the application, and which ones to refuse.
optional_scope

A list of space-separated optional access permissions that the application can function without. Optional permissions are requested in addition to the permissions specified in the scope parameter. Optional permissions must be from the list that was specified when registering the application. To find out which permissions are available, go to https://oauth.yandex.com/client/<client_id>/info (specify the application's ID in place of <client_id>).

If the scope and optional_scope parameters were omitted from the request, the token is issued with the permissions that were specified when registering the application.

The user can decide which of the requested permissions to grant the application, and which ones to refuse. The token is issued with the permissions specified in the scope parameter and the permissions from the optional_scope parameter that the user selected.

For example, you can use this parameter if the application requires the user's email address for registration, while access to the user's profile picture is preferable, but not required.

Note. Access permissions that are requested using both the scope parameter and the optional_scope parameter simultaneously are treated as optional permissions.
force_confirm

Indicates that the application must ask the user for permission to access the account (even if the user has already granted access for this application). When it gets this parameter, Yandex.OAuth asks the user to allow the application access and to select the desired Yandex account.

This parameter is processed if the value is set to “yes”, “true”, or “1”. If any other value is set, the parameter is ignored.

state

The state string, which Yandex.OAuth returns without making any changes. The maximum length of the string is 1024 characters.

For example, you can use it for protection from CSRF attacks or for identifying the user that a token is requested for.

Response from Yandex.OAuth

Token data is passed in the redirect URL, after the # symbol.

URL format with a token:

myapp://token#
   access_token=<new OAuth token>
 & expires_in=<token lifespan in seconds>
 & token_type=bearer
[& state=<value of the state parameter passed in the request>]
ParameterDescription
access_token

The OAuth token with the requested permissions, or with the permissions specified when registering the application.

expires_in

The token lifespan in seconds.

token_type

Type of token issued. Always takes the value “bearer”.

state

The value of the state parameter from the source request, if this parameter was passed.

If a token could not be issued, Yandex.OAuth appends the error code and description to the address. The description is in the language of the OAuth domain that the request was sent to. For example, for oauth.yandex.com, text will be returned in English.

myapp://token#
  state=<value of the state parameter in the request>
& error=<error code>

Possible error codes:

  • access_denied — The user denied the application access.
  • unauthorized_client — The application was rejected during moderation, or moderation is pending. This code is also returned if the application is blocked.