Expand my Community achievements bar.

SOLVED

Reporting API 2.0: Bearer Token

Avatar

Level 2

Is any API available to automate the Adobe IMS login to fetch the auth code? I am looking to see how can I automate the process of fetching the access token for adobe analytics reporting API 2.0

 

I place I am stuck is the bearer token use to expire and I want to refresh the token dynamically.

 

Is there a way out for this?

 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

HI @AnkitAg3 

do you really need to refresh the token if you are using the server-side credentials in your API?

bjoern__koth_0-1728301920706.png

 

Typically these settings should suffice

bjoern__koth_1-1728301984070.png

 

 

Cheers from Switzerland!


View solution in original post

3 Replies

Avatar

Correct answer by
Community Advisor

HI @AnkitAg3 

do you really need to refresh the token if you are using the server-side credentials in your API?

bjoern__koth_0-1728301920706.png

 

Typically these settings should suffice

bjoern__koth_1-1728301984070.png

 

 

Cheers from Switzerland!


Avatar

Community Advisor and Adobe Champion

Hi @AnkitAg3, guessing what is the "automate" you are looking for. As mentioned by @bjoern__koth, the server-to-server authentication is completely automated. The user oauth is using the identification of who is logging in, so there is not "automate" process and must have someone to log in manually.

Avatar

Level 4

Hi @AnkitAg3 
Adobe API token will expiry after 24 hours
You can use this code fetch bearer token dynamically.

let accessToken = null;
let tokenExpirationTime = null;

// Function to fetch a new Bearer token
async function fetchBearerToken() {
const response = await fetch('https://example.com/api/token', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
// Include any required credentials or additional headers
},
body: JSON.stringify({
// Include required parameters for token retrieval
username: 'yourUsername',
password: 'yourPassword'
})
});

if (!response.ok) {
throw new Error('Failed to fetch token');
}

const data = await response.json();
accessToken = data.token; // Assuming the token is in `data.token`
const expiresIn = data.expiresIn; // Assuming expiration is provided in seconds
tokenExpirationTime = Date.now() + expiresIn * 1000; // Convert to milliseconds
}

// Function to get the Bearer token, refreshing if necessary
async function getBearerToken() {
const isTokenExpired = !accessToken || Date.now() >= tokenExpirationTime;

if (isTokenExpired) {
await fetchBearerToken();
}

return accessToken;
}

// Example usage in an API request
async function makeApiRequest() {
const token = await getBearerToken();

const response = await fetch('https://example.com/api/protected-resource', {
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
}
});

if (!response.ok) {
throw new Error('API request failed');
}

const data = await response.json();
return data; // Process your data as needed
}

// Call the API
makeApiRequest()
.then(data => console.log(data))
.catch(error => console.error(error));