Exchange Code for Tokens
curl --request POST \
--url https://api.example.com/v1/auth/oauth/token \
--header 'Content-Type: application/json' \
--data '
{
"grant_type": "<string>",
"code": "<string>",
"redirect_uri": "<string>",
"code_verifier": "<string>",
"refresh_token": "<string>"
}
'import requests
url = "https://api.example.com/v1/auth/oauth/token"
payload = {
"grant_type": "<string>",
"code": "<string>",
"redirect_uri": "<string>",
"code_verifier": "<string>",
"refresh_token": "<string>"
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
grant_type: '<string>',
code: '<string>',
redirect_uri: '<string>',
code_verifier: '<string>',
refresh_token: '<string>'
})
};
fetch('https://api.example.com/v1/auth/oauth/token', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.example.com/v1/auth/oauth/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'grant_type' => '<string>',
'code' => '<string>',
'redirect_uri' => '<string>',
'code_verifier' => '<string>',
'refresh_token' => '<string>'
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.example.com/v1/auth/oauth/token"
payload := strings.NewReader("{\n \"grant_type\": \"<string>\",\n \"code\": \"<string>\",\n \"redirect_uri\": \"<string>\",\n \"code_verifier\": \"<string>\",\n \"refresh_token\": \"<string>\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.example.com/v1/auth/oauth/token")
.header("Content-Type", "application/json")
.body("{\n \"grant_type\": \"<string>\",\n \"code\": \"<string>\",\n \"redirect_uri\": \"<string>\",\n \"code_verifier\": \"<string>\",\n \"refresh_token\": \"<string>\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.example.com/v1/auth/oauth/token")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/json'
request.body = "{\n \"grant_type\": \"<string>\",\n \"code\": \"<string>\",\n \"redirect_uri\": \"<string>\",\n \"code_verifier\": \"<string>\",\n \"refresh_token\": \"<string>\"\n}"
response = http.request(request)
puts response.read_body{
"access_token": "<string>",
"expires_in": 123,
"refresh_token": "<string>",
"refresh_token_expires_in": 123
}Authentication
Exchange Code for Tokens
Final OAuth step - exchange authorization code or refresh token for access tokens
POST
/
v1
/
auth
/
oauth
/
token
Exchange Code for Tokens
curl --request POST \
--url https://api.example.com/v1/auth/oauth/token \
--header 'Content-Type: application/json' \
--data '
{
"grant_type": "<string>",
"code": "<string>",
"redirect_uri": "<string>",
"code_verifier": "<string>",
"refresh_token": "<string>"
}
'import requests
url = "https://api.example.com/v1/auth/oauth/token"
payload = {
"grant_type": "<string>",
"code": "<string>",
"redirect_uri": "<string>",
"code_verifier": "<string>",
"refresh_token": "<string>"
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
grant_type: '<string>',
code: '<string>',
redirect_uri: '<string>',
code_verifier: '<string>',
refresh_token: '<string>'
})
};
fetch('https://api.example.com/v1/auth/oauth/token', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.example.com/v1/auth/oauth/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'grant_type' => '<string>',
'code' => '<string>',
'redirect_uri' => '<string>',
'code_verifier' => '<string>',
'refresh_token' => '<string>'
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.example.com/v1/auth/oauth/token"
payload := strings.NewReader("{\n \"grant_type\": \"<string>\",\n \"code\": \"<string>\",\n \"redirect_uri\": \"<string>\",\n \"code_verifier\": \"<string>\",\n \"refresh_token\": \"<string>\"\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.example.com/v1/auth/oauth/token")
.header("Content-Type", "application/json")
.body("{\n \"grant_type\": \"<string>\",\n \"code\": \"<string>\",\n \"redirect_uri\": \"<string>\",\n \"code_verifier\": \"<string>\",\n \"refresh_token\": \"<string>\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.example.com/v1/auth/oauth/token")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/json'
request.body = "{\n \"grant_type\": \"<string>\",\n \"code\": \"<string>\",\n \"redirect_uri\": \"<string>\",\n \"code_verifier\": \"<string>\",\n \"refresh_token\": \"<string>\"\n}"
response = http.request(request)
puts response.read_body{
"access_token": "<string>",
"expires_in": 123,
"refresh_token": "<string>",
"refresh_token_expires_in": 123
}Overview
Exchange an authorization code or refresh token for access tokens. Supports two grant types:- Authorization Code (
grant_type=authorization_code) - Step 4 of OAuth flow - Refresh Token (
grant_type=refresh_token) - Renew expired access tokens
Request
Body Parameters
string
required
Type of token exchangeValues:
authorization_code | refresh_tokenstring
Authorization code from Step 3Required for:
authorization_code grantstring
Must exactly match URI from Step 1Required for:
authorization_code grantstring
Original PKCE verifier from Step 1 (43-128 chars)Required for:
authorization_code grantPattern: [A-Za-z0-9-._~]{43,128}string
Refresh token from previous exchangeRequired for:
refresh_token grantResponse
string
Bearer token for API authentication (6 hours expiry)Example:
access_token_100a99cf-f4d3-4fa1-9be9-2e9828b20ebcnumber
Access token lifetime in seconds (21600 = 6 hours)
string
Token for obtaining new access tokens (184 days expiry)Example:
refresh_token_100a99cf-f4d3-4fa1-9be9-2e9828b20ebdnumber
Refresh token lifetime in seconds (15897600 = 184 days)
Code Examples
curl -X POST "https://dev.api.baanx.com/v1/auth/oauth/token" \
-H "x-client-key: your-client-key" \
-H "Content-Type: application/json" \
-d '{
"grant_type": "authorization_code",
"code": "auth_code_xyz123",
"redirect_uri": "https://yourapp.com/callback",
"code_verifier": "bbdbb44b57e78fbdf7254757bc62de8ce2b5342c"
}'
curl -X POST "https://dev.api.baanx.com/v1/auth/oauth/token" \
-H "x-client-key: your-client-key" \
-H "Content-Type: application/json" \
-d '{
"grant_type": "refresh_token",
"refresh_token": "refresh_token_abc123"
}'
async function getValidToken() {
let accessToken = localStorage.getItem('access_token');
const expiresAt = parseInt(localStorage.getItem('token_expires_at'));
// Check if token expired or about to expire (5 min buffer)
if (Date.now() >= expiresAt - 300000) {
const refreshToken = localStorage.getItem('refresh_token');
const response = await fetch('https://dev.api.baanx.com/v1/auth/oauth/token', {
method: 'POST',
headers: {
'x-client-key': 'your-client-key',
'Content-Type': 'application/json'
},
body: JSON.stringify({
grant_type: 'refresh_token',
refresh_token: refreshToken
})
});
const data = await response.json();
// Store new tokens
accessToken = data.access_token;
localStorage.setItem('access_token', data.access_token);
localStorage.setItem('refresh_token', data.refresh_token);
localStorage.setItem('token_expires_at', Date.now() + data.expires_in * 1000);
}
return accessToken;
}
Implement automatic token refresh 5 minutes before expiry to ensure uninterrupted API access.
Was this page helpful?
⌘I