My challenge lies in utilizing node-fetch to retrieve the bearer token within a function. As I attempt to tackle this issue, the code snippet looks like this:
import fetch from 'node-fetch'
export class APIToken {
async getToken() {
const tenant_id = '123';
const formData = new URLSearchParams();
formData.append('grant_type', 'password');
formData.append('scope', 'api://');
formData.append('client_secret', '123');
formData.append('client_id', '456');
formData.append('username', '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b6c3c5d3c4f6d3dbd7dfda98d5d9db">[email protected]</a>');
formData.append('password', 'password!!');
const response = await fetch(`https://login.microsoftonline.com//${tenant_id}//oauth2/token`, {
method: 'POST',
body: formData
});
const responseBody = await response.json();
const accessToken = responseBody.access_token;
return accessToken
}
}
This yields the message:
responseBody is of type 'unknown'
Specifically occurring on the line:
const accessToken = responseBody.access_token;