The app requests a code

Getting a token in exchange for a code extracted from a URL:
  1. The app directs the user to the Yandex.OAuth page, where they can allow access to their data.

  2. The user allows access to the app.

  3. Yandex.OAuth redirects the user to the URL specified in the Callback URL field when registering the app. The confirmation code (or error description) is passed in the redirect URL parameter.

  4. The app gets the forwarding address and retrieves the confirmation code.

  5. The app sends a POST request with the code.

  6. Yandex.OAuth returns a token in the response body.

Tip. You can get debug tokens for the app manually.

The received token can be saved in the app and used for requests until its lifetime expires. The token should be available only to your app, so we don't recommend saving it in browser cookies, open configuration files, and so on.

Confirmation code request

The app should direct the user to Yandex.OAuth at the following URL:

https://oauth.yandex.com/authorize?
   response_type=code
 & client_id=<идентификатор приложения>
[& device_id=<идентификатор устройства>]
[& device_name=<имя устройства>]
[& redirect_uri=<адрес перенаправления>]
[& login_hint=<имя пользователя или электронный адрес>]
[& scope=<запрашиваемые необходимые права>]
[& optional_scope=<запрашиваемые опциональные права>]
[& force_confirm=yes]
[& state=<произвольная строка>]
Parameter Description
Required parameters

response_type

Expected response.

When you request the confirmation code, specify the “code” value.

client_id

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

Advanced parameters
device_id

Unique ID of the device the token is requested for. To ensure uniqueness, just generate a UUID once and use it every time a new token is requested from this device.

The ID must be from 6 to 50 characters long. Only printable ASCII characters are allowed (with codes from 32 to 126).

Restriction. An app can have up to 20 tokens linked to a user's devices. If Yandex.OAuth issues a new device token for the app, the oldest token stops working.

Learn more about tokens for individual devices at Token for a device.

device_name

The name of the device to show users. Up to 100 characters.

For mobile devices, we recommend passing the device name specified by the user. If a name is missing, the name can be taken from the device model, OS name and version, and so on.

If the device_name parameter is sent without the device_id parameter, it is ignored. Yandex.OAuth can only issue a regular token that is not linked to a device.

If the device_id parameter is sent without the device_name parameter, the token is marked as issued for an unknown device in the user interface.

redirect_uri

The URL to redirect the user to after they allow or deny access to the app. By default, the first Callback URI specified in the app settings is used (Platforms → Web services → Callback URI).

The parameter value can only contain URLs listed in the app settings. If the match isn't exact, this parameter is ignored.

login_hint

Explicit indication of the account the token is requested for. The Yandex account username, as well as the Yandex.Mail or Yandex.Mail for Domain address, can be passed in the parameter value.

This parameter allows you to help the user log in to Yandex with the account that the app needs access to. When Yandex.OAuth receives this parameter, it checks the user's authorization:

  • If the user is already logged in with the appropriate account, Yandex.OAuth just asks for access permission.

  • If the user isn't logged in with the appropriate account, they see the Yandex login form where the login field is filled in with the parameter value. Remember that the token won't necessarily be requested for the specified account: the user can erase the pre-filled username and log in with any other.

If this parameter indicates a nonexistent account, Yandex.OAuth will only be able to inform the user. The app will have to request the token again.

scope

A list of access rights currently required by the application, separated by a space. Rights must be requested from the list defined when registering the application. You can find out the allowed rights at the link https://oauth.yandex.com/client/<client_id>/info, specifying the application ID instead of <client_id>.

If the scope and optional_scope parameters aren't passed, the token will be issued with the rights specified when registering the application.

This parameter allows you to get a token only with the rights currently required by the app.

Note. All access rights are requested at once via the scope and optional_scope parameters, are considered optional, and are not required for the app to function. The user decides which requested optional rights to grant.
optional_scope

A list of optional access rights, separated by a space, that aren't required for the app functionality. Optional rights are requested in addition to the rights specified in the scope parameter. Optional rights must be requested from the list defined when registering the application. You can find out the allowed rights at the link https://oauth.yandex.com/client/<client_id>/info, specifying the application ID instead of <client_id>.

If the scope and optional_scope parameters aren't passed, the token will be issued with the rights specified when registering the application.

The user decides which requested optional rights to grant. The token will be issued with the rights specified in the scope parameter, and the rights selected by the user from the list specified in the optional_scope parameter.

This parameter can be used, for example, if the app needs an email to register the user, and access to the portrait is preferred, but not required.

Note. All access rights requested at once via the scope and optional_scope parameters are considered optional.
force_confirm

Indicates that the user must request permission to access the account (even if the user already allowed access to this app). After receiving this parameter, Yandex.OAuth will prompt the user to allow access to the application and choose the Yandex account.

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

state

The status bar that Yandex. OAuth returns without changes. The maximum allowed string length is 1024 characters.

Can be used, for example, to protect against CSRF attacks or identify the user the token is requested for.

Parameter Description
Required parameters

response_type

Expected response.

When you request the confirmation code, specify the “code” value.

client_id

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

Advanced parameters
device_id

Unique ID of the device the token is requested for. To ensure uniqueness, just generate a UUID once and use it every time a new token is requested from this device.

The ID must be from 6 to 50 characters long. Only printable ASCII characters are allowed (with codes from 32 to 126).

Restriction. An app can have up to 20 tokens linked to a user's devices. If Yandex.OAuth issues a new device token for the app, the oldest token stops working.

Learn more about tokens for individual devices at Token for a device.

device_name

The name of the device to show users. Up to 100 characters.

For mobile devices, we recommend passing the device name specified by the user. If a name is missing, the name can be taken from the device model, OS name and version, and so on.

If the device_name parameter is sent without the device_id parameter, it is ignored. Yandex.OAuth can only issue a regular token that is not linked to a device.

If the device_id parameter is sent without the device_name parameter, the token is marked as issued for an unknown device in the user interface.

redirect_uri

The URL to redirect the user to after they allow or deny access to the app. By default, the first Callback URI specified in the app settings is used (Platforms → Web services → Callback URI).

The parameter value can only contain URLs listed in the app settings. If the match isn't exact, this parameter is ignored.

login_hint

Explicit indication of the account the token is requested for. The Yandex account username, as well as the Yandex.Mail or Yandex.Mail for Domain address, can be passed in the parameter value.

This parameter allows you to help the user log in to Yandex with the account that the app needs access to. When Yandex.OAuth receives this parameter, it checks the user's authorization:

  • If the user is already logged in with the appropriate account, Yandex.OAuth just asks for access permission.

  • If the user isn't logged in with the appropriate account, they see the Yandex login form where the login field is filled in with the parameter value. Remember that the token won't necessarily be requested for the specified account: the user can erase the pre-filled username and log in with any other.

If this parameter indicates a nonexistent account, Yandex.OAuth will only be able to inform the user. The app will have to request the token again.

scope

A list of access rights currently required by the application, separated by a space. Rights must be requested from the list defined when registering the application. You can find out the allowed rights at the link https://oauth.yandex.com/client/<client_id>/info, specifying the application ID instead of <client_id>.

If the scope and optional_scope parameters aren't passed, the token will be issued with the rights specified when registering the application.

This parameter allows you to get a token only with the rights currently required by the app.

Note. All access rights are requested at once via the scope and optional_scope parameters, are considered optional, and are not required for the app to function. The user decides which requested optional rights to grant.
optional_scope

A list of optional access rights, separated by a space, that aren't required for the app functionality. Optional rights are requested in addition to the rights specified in the scope parameter. Optional rights must be requested from the list defined when registering the application. You can find out the allowed rights at the link https://oauth.yandex.com/client/<client_id>/info, specifying the application ID instead of <client_id>.

If the scope and optional_scope parameters aren't passed, the token will be issued with the rights specified when registering the application.

The user decides which requested optional rights to grant. The token will be issued with the rights specified in the scope parameter, and the rights selected by the user from the list specified in the optional_scope parameter.

This parameter can be used, for example, if the app needs an email to register the user, and access to the portrait is preferred, but not required.

Note. All access rights requested at once via the scope and optional_scope parameters are considered optional.
force_confirm

Indicates that the user must request permission to access the account (even if the user already allowed access to this app). After receiving this parameter, Yandex.OAuth will prompt the user to allow access to the application and choose the Yandex account.

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

state

The status bar that Yandex. OAuth returns without changes. The maximum allowed string length is 1024 characters.

Can be used, for example, to protect against CSRF attacks or identify the user the token is requested for.

Getting a confirmation code from a URL

When the user allows access to their data, Yandex.OAuth redirects them to the app. The confirmation code is included in the redirect URL.

Note. The lifetime of this code is 10 minutes. After this time, the code must be requested again.

Format of the URL with code:

http://www.example.com/token?
   code=<код подтверждения>
[& state=<значение параметра state в запросе>]
Parameter Description
code

Confirmation code that can be exchanged for an OAuth token.

The lifetime of this code is 10 minutes. After this time, the code must be requested again.

state

The status bar that Yandex. OAuth returns without changes. The maximum allowed string length is 1024 characters.

Can be used, for example, to protect against CSRF attacks or identify the user the token is requested for.

Parameter Description
code

Confirmation code that can be exchanged for an OAuth token.

The lifetime of this code is 10 minutes. After this time, the code must be requested again.

state

The status bar that Yandex. OAuth returns without changes. The maximum allowed string length is 1024 characters.

Can be used, for example, to protect against CSRF attacks or identify the user the token is requested for.

If the confirmation code couldn't be issued, Yandex.OAuth specifies the error code and description in the URL. The description is in the language of the OAuth domain the request was sent to: for example, for oauth.yandex.com, the text will be returned in English.

Format of the URL with error information:

http://www.example.com/token?
   error=<код ошибки>
 & error_description=<описание ошибки>
[& state=<значение параметра state в запросе>]

Error codes:

  • access_denied: The user denied access to the app.
  • unauthorized_client: The app was rejected during moderation or is awaiting moderation. Also returned if the app is blocked.

Exchanging a confirmation code for a token

The app sends the code as well as its ID and password in a POST request.

POST /token HTTP/1.1
Host: oauth.yandex.com
Content-type: application/x-www-form-urlencoded
Content-Length: <длина тела запроса>
[Authorization: Basic <закодированная строка client_id:client_secret>]

   grant_type=authorization_code
 & code=<код подтверждения>
[& client_id=<идентификатор приложения>]
[& client_secret=<пароль приложения>]
[& device_id=<идентификатор устройства>]
[& device_name=<имя устройства>]
Parameter Description
Required parameters

grant_type

The method used to request the OAuth token.

If you use a confirmation code, specify the “authorization_code” value.

code

Confirmation code received from Yandex.OAuth. The lifetime of this code is 10 minutes. After this time, the code must be requested again.

Advanced parameters

client_id

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

client_secret

App password. Available in application properties (click the app name to open its properties).

You can also pass the password and app ID in the authorization header.

device_id

ID of the device that the token is requested for.

If the ID was specified in the device_id parameter when requesting a confirmation code, the device_id and device_name parameters are ignored when requesting a token.

device_name

The name of the device the token is requested for.

If the device_id parameter was passed when requesting the confirmation code, the device_id and device_name parameters are ignored when requesting the token.

Parameter Description
Required parameters

grant_type

The method used to request the OAuth token.

If you use a confirmation code, specify the “authorization_code” value.

code

Confirmation code received from Yandex.OAuth. The lifetime of this code is 10 minutes. After this time, the code must be requested again.

Advanced parameters

client_id

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

client_secret

App password. Available in application properties (click the app name to open its properties).

You can also pass the password and app ID in the authorization header.

device_id

ID of the device that the token is requested for.

If the ID was specified in the device_id parameter when requesting a confirmation code, the device_id and device_name parameters are ignored when requesting a token.

device_name

The name of the device the token is requested for.

If the device_id parameter was passed when requesting the confirmation code, the device_id and device_name parameters are ignored when requesting the token.

You can also send the app ID and password in the Authorization header by encoding the client_id>:<client_secret> string with the Base64 method. If Yandex.OAuth receives the Authorization header, the client_id and client_secret parameters in the request body are ignored.

Format of response with token

Yandex.OAuth returns the OAuth token, refresh token, and their lifetime in JSON format:

200 OK
Content-type: application/json

{
  "token_type": "bearer",
  "access_token": "AQAAAACy1C6ZAAAAfa6vDLuItEy8pg-iIpnDxIs",
  "expires_in": 124234123534,
  "refresh_token": "1:GN686QVt0mmakDd9:A4pYuW9LGk0_UnlrMIWklkAuJkUWbq27loFekJVmSYrdfzdePBy7:A-2dHOmBxiXgajnD-kYOwQ",
  "scope": "login:info login:email login:avatar"
}
Property Description
token_type

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

access_token

An OAuth token with the requested rights or with the rights specified when registering the app.

expires_in

Token lifetime in seconds.

refresh_token

A token that can be used to extend the lifetime of the corresponding OAuth token.

scope

Rights requested by the developer or specified when registering the app. The scope field is optional and is returned if OAuth provided a token with a smaller set of rights than requested.

Property Description
token_type

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

access_token

An OAuth token with the requested rights or with the rights specified when registering the app.

expires_in

Token lifetime in seconds.

refresh_token

A token that can be used to extend the lifetime of the corresponding OAuth token.

scope

Rights requested by the developer or specified when registering the app. The scope field is optional and is returned if OAuth provided a token with a smaller set of rights than requested.

The lifetime of the refresh token is the same as the OAuth token lifetime.

If a token couldn't be issued, the response contains a description of the error:

{
  "error_description": "<error description>",
  "error": "<error code>"
}

Error codes:

  • authorization_pending: The user didn't enter the confirmation code yet.
  • bad_verification_code: The passed code parameter value isn't a 7-digit number.
  • invalid_client: The app with the specified ID (the client_id parameter) wasn't found or is blocked. This code is also returned if the client_secret parameter passed an invalid app password.
  • invalid_grant: Invalid or expired confirmation code.
  • invalid_request: Invalid request format (one of the parameters isn't specified, specified twice, or isn't passed in the request body).
  • invalid_scope: The app rights changed after the confirmation code was generated.
  • unauthorized_client: The app was rejected during moderation or is awaiting moderation.
  • unsupported_grant_type: Invalid grant_type parameter value.
  • Basic auth required: The authorization type specified in the Authorization header is not Basic.
  • Malformed Authorization header: The Authorization header isn't in <client_id>:<client_secret> format, or this string isn't Base64-encoded.