Get Access Token
Learn how to use the flows, also known as grant types, to obtain an Access Token and access the data exposed by an API. The existence of these flows arise to respond to all business scenarios that can appear in the consumption of APIs based on the type of consuming application, its degree of trust, and how the user interacts in the process.
The access flows available for generating the Access Token are:
- Authorization code: when credentials are to be used to access a resource on behalf of others.
- Client credentials: when credentials are to be used to access a resource on one's own behalf.
Authorization code
The flow is characterized by the intervention of the seller to explicitly authorize the application's access to their data and by the use of a code granted by the authentication server so that the application can obtain an Access Token and an associated refresh token.
Because it is a redirect-based flow, you must allow interaction with the seller's browser and receive the request through the authorization server redirect. In this flow, the application requests the seller's express consent to access the data by opening a web page in which the requested areas to be accessed are made explicit.
Once access is allowed, the server generates an access code that reaches the application through a redirect. In this step, the application requests access to the authentication server by sending the obtained code and application data. Once this is done, the server grants the Access Token and the refresh token to the application.
See below how to configure the PKCE protocol (a non-mandatory security protocol that provides an extra layer of protection, so it is recommended) and then generate the Access Token.
Configure PKCE
The PKCE (Proof Key for Code Exchange) is a security protocol used with OAuth to protect against malicious code attacks during the exchange of authorization codes for an Access Token. It adds an extra layer of security by generating a verifier that is transformed into a challenge to ensure that even if the authorization code is intercepted, it is not useful without the original verifier.
Follow the steps below to enable and configure the use of the authorization code flow with PKCE.
- First, on the Application details screen, click Edit and enable the use of the authorization code flow with PKCE. With the field enabled, Mercado Pago will require the
code_challenge
andcode_method
fields in OAuth requests. - The fields can be generated in various ways, either through custom development or using SDKs. Follow the necessary steps described in this official documentation to generate the required fields.
- After generating and encrypting the fields, it will be necessary to send the respective codes to Mercado Pago. To do this, send them via
query_params
using the authentication URL below.
URL
https://auth.mercadopago.com/authorization?response_type=code&client_id=$APP_ID`redirect_uri=$YOUR_URL&code_challenge=$CODE_CHALLENGE&code_challenge_method=$CODE_METHOD
- Redirect_uri: URL provided in the "Redirect URL" field of your application.
- Code_verifier: code that should be generated, following the requirements for its functionality, which include: a random sequence of characters with a length between 43 and 128 characters, including uppercase letters, lowercase letters, numbers, and some special characters. For example: 47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU.
- Code_challenge: next, it is necessary to create a
code_challenge
from thecode_verifier
using one of the following transformations:- If it's possible to use S256, it will be necessary to use this option by transforming the
code_verifier
into acode_challenge
throughBASE64URL
encoding after applying the "SHA256" function. - If it's not possible to use S256 for some technical reason and the server supports the Plain method, it's possible to set the c
ode_challenge
equal to thecode_verifier
.
- If it's possible to use S256, it will be necessary to use this option by transforming the
- Code_challenge_method: is the method used to generate the
code_challenge
, as described in the above item. This field can be, for example, S256 or Plain, depending on the encoding selected in thecode_challenge stage
.
- After correctly sending the codes to Mercado Pago, you will obtain the necessary authorization (
code_verifier
) for get the Access Token and perform PKCE verification on transactions made with OAuth.
Get token
Access Token is the code used in different requests of public origin to access a protected resource. In this flow, that represents an authorization granted by a seller to a client application that contains scopes and a limited period of time for such access. Follow the steps below to obtain it.
Edit your application so that it contains your Redirect URL. See Edit application.
Send the authentication URL to the seller whose account you want to link to yours with the following fields:
Authentication_URL
https://auth.mercadopago.com/authorization?client_id=APP_ID&response_type=code&platform_id=mp&state=RANDOM_ID&redirect_uri=https://www.redirect-url.com
Field Description Client_id Replace the "APP_ID" value with your application number. Check Application ID for more information. State Replace the "RANDOM_ID" value with an identifier that is unique for each attempt and does not include sensitive information so that you can identify who the received code is from. This way, you can ensure that the response belongs to a request initiated by the same application. Redirect_uri Add the reported URL in the "Redirect URLs" field of your application. Make sure that the redirect_uri is a static URL Check Application ID for more information. If you want to send additional parameters in theredirect_uri
, use thestate
parameter to include that information. Otherwise, the call will receive an error response if the URL does not exactly match the application's configuration.Wait for the seller to access the URL and allow access. Upon accessing the URL, the seller will be directed to Mercado Pago and must log into their account to carry out the authorization.
Check your server's Redirect URL to see the authorization code returned in the code parameter.
Redirect_URL
https://www.redirect-url.com?code=CODE&state=RANDOM_ID
Send your credentials (
client_id
andclient_secret
), the authorization code (code
) returned and, if you have configured the PKCE, thecode_verifier
to the /oauth/token endpoint to receive the Access Token in response.
<?php
$client = new OauthClient();
$request = new OAuthCreateRequest();
$request->client_secret = "CLIENT_SECRET";
$request->client_id = "CLIENT_ID";
$request->code = "CODE";
$request->redirect_uri = "REDIRECT_URI";
$client->create($request);
?>
OauthClient client = new OauthClient();
String authorizationCode = "TG-XXXXXXXX-241983636";
client.createCredential(authorizationCode, null);
const client = new MercadoPagoConfig({ accessToken: 'access_token', options: { timeout: 5000 } });
const oauth = new OAuth(client);
oauth.create({
'client_secret': 'your-client-secret',
'client_id': 'your-client-id',
'code': 'return-of-getAuthorizationURL-function',
'redirect_uri': 'redirect-uri'
}).then((result) => console.log(result))
.catch((error) => console.log(error));
curl -X POST \
'https://api.mercadopago.com/oauth/token'\
-H 'Content-Type: application/json' \
-d '{
"client_id": "client_id",
"client_secret": "client_secret",
"code": "TG-XXXXXXXX-241983636",
"grant_type": "authorization_code",
"redirect_uri": "https://www.redirect-url.com",
"refresh_token": "TG-XXXXXXXX-241983636",
"test_token": "false"
}'
test_token
parameter with the value true
.Client credentials
This flow is used when applications request an Access Token using only their own credentials and to access their own resources. The main difference compared to other flows is that the user does not interact in the process, and consequently, the application cannot act on behalf of the user.
Get token
Access Token is the code used in different requests of public origin to access a protected resource. In this flow, the Access Token is obtained without user interaction and only to access the application's own resources.
Follow the steps below to obtain it.
- Send your credentials (
client_id
andclient_secret
) to the /oauth/token endpoint with theclient_credentials
code in thegrant_type
parameter to receive a new response with a newaccess_token
. - Update the application with the Access Token received in the response.
<?php
$client = new OauthClient();
$request = new OAuthCreateRequest();
$request->client_secret = "CLIENT_SECRET";
$request->client_id = "CLIENT_ID";
$client->create($request);
?>
const client = new MercadoPagoConfig({ accessToken: 'access_token', options: { timeout: 5000 } });
const oauth = new OAuth(client);
oauth.create({
'client_secret': 'your-client-secret',
'client_id': 'your-client-id',
}).then((result) => console.log(result))
.catch((error) => console.log(error));
curl -X POST \
'https://api.mercadopago.com/oauth/token'\
-H 'Content-Type: application/json' \
-d '{
"client_id": "client_id",
"client_secret": "client_secret",
"grant_type": "client_credentials",
}'