OAuth2.0 - Refresh or renew the access token
POST https://bakrypt.io/auth/token/
|| https://testnet.bakrypt.io/auth/token/
;
Some time has passed, and your access token is about to expire; you can get renew the access token issued using the refresh token:
e.g.,
import express from 'express';
import axios from 'axios';
interface AccessToken {
access_token: string;
expires_in: number;
token_type: string;
scope: string;
refresh_token: string;
}
interface ErrorResponse {
error: string;
error_description?: string;
}
const app = express();
const externalApiUrl = 'https://bakrypt.io/auth/token/' || 'https://testnet.bakrypt.io/auth/token/';
const clientId = `$client_id`;
const clientSecret = `$client_secret`;
app.use(express.json());
app.post('/refresh', async (req, res) => {
try {
const { refreshToken } = req.body;
const payload = new URLSearchParams();
payload.append("client_id", clientId);
payload.append("client_secret", clientSecret);
payload.append("refresh_token", refreshToken);
payload.append("grant_type", "refresh_token");
const config = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
};
const response = await axios.post<AccessToken | ErrorResponse>(externalApiUrl, payload, config);
res.send(response.data);
} catch (error) {
console.error(error);
// Handle error accordingly
res.status(500).send('Internal Server Error');
}
});
app.listen(3000, () => {
console.log('Server is listening on port 3000');
});
import requests
try:
from typing import TypedDict
except Exception as e:
from typing_extensions import TypedDict
class AccessToken(TypedDict):
access_token: str
expires_in: int
token_type: str
scope: str
refresh_token: str
class ErrorResponse(TypedDict):
error: str
error_description: str
CLIENT_ID = '$client_id'
CLIENT_SECRET = '$client_secret'
REFRESH_TOKEN = '$resource_refresh_token'
payload = {
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
'refresh_token': REFRESH_TOKEN,
'grant_type': 'refresh_token'
}
access_token_request = requests.post(
'https://testnet.bakrypt.io/auth/token/',
data=payload,
headers={'content-type': "application/x-www-form-urlencoded"})
if access_token_request.status_code in [200]:
access_token: AccessToken = access_token_request.json()
print(access_token)
else:
print("Error retrieving access token...")
print(access_token_request.text)