The app requests a code
The app directs the user to the Yandex.OAuth page, where they can allow access to their data.
The user allows access to the app.
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.
The app gets the forwarding address and retrieves the confirmation code.
The app sends a POST request with the code.
Yandex.OAuth returns a token in the response body.
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=<app id>
[& device_id=<device id>]
[& device_name=<device name>]
[& redirect_uri=<redirect address>]
[& login_hint=<username or email address>]
[& scope=<requested required rights>]
[& optional_scope=<requested optional rights>]
[& force_confirm=yes]
[& state=<arbitrary string>]
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 If the |
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 ( ).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 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 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 If the The user decides which requested optional rights to grant. The token will be issued with the rights specified in the 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 If the |
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 ( ).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 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 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 If the The user decides which requested optional rights to grant. The token will be issued with the rights specified in the 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.
Format of the URL with code:
http://www.example.com/token?
code=<confirmation code>
[& state=<state parameter value in the request>]
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 code>
& error_description=<error description>
[& state=<state parameter value in the request>]
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: <
request body length>
[Authorization: Basic <
encoded string client_id:client_secret
>
]
grant_type=authorization_code
& code=<confirmation code>
[& client_id=<app ID>]
[& client_secret=<app password>]
[& device_id=<device ID>]
[& device_name=<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). You can also pass the password and app ID in the authorization header. |
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_name | The name of the device the token is requested for. If the |
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). You can also pass the password and app ID in the authorization header. |
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_name | The name of the device the token is requested for. If the |
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 |
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 |
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 passedcode
parameter value isn't a 7-digit number.invalid_client
: The app with the specified ID (theclient_id
parameter) wasn't found or is blocked. This code is also returned if theclient_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
: Invalidgrant_type
parameter value.Basic auth required
: The authorization type specified in theAuthorization
header is notBasic
.Malformed Authorization header
: TheAuthorization
header isn't in<client_id>:<client_secret>
format, or this string isn't Base64-encoded.