Proper Token passing

I am working in Node.js and am able to get a POST request to get a token ok in the format below

{
"access_token": "9s1jsCNxpF1swHGQGmXXZhgpQTPpxII6BYAwtlNuEUxD0614nDBmMcGLs2zzTBJeJQJZbgHeZ3QviEoXXXXXX",
"token_type": "bearer",
"expires_in": 86399
}

When i then pass that to another app in Node using the SDK
function getMe(yourAccessToken) {

platformClient.ApiClient.instance.setAccessToken(yourAccessToken);
/
let apiInstance = new platformClient.TokensApi();
apiInstance.getTokensMe()
.then((data) => {
console.log(getTokensMe success! data: ${JSON.stringify(data, null, 2)});
})
.catch((err) => {
console.log('There was a failure calling getTokensMe');
//console.error(err);
})
}
i get the error

body:
{ status: 401,
code: 'authentication.required',
message: 'No authentication bearer token specified in authorization header.
},

from the SDK article located https://developer.mypurecloud.com/api/rest/client-libraries/javascript/TokensApi.html#getTokensMe
i was hoping that i just pass the variable that has the token and that hosts it. Let me down gently if i am missing a step :slight_smile:

Nice token format. It's very useful more than what I aspect.
Thanks

I just put together a quick test app for this and it's working correctly. There is some issue in your code that's causing the auth token not to be set correctly.

const platformClient = require('purecloud-platform-client-v2');

const client = platformClient.ApiClient.instance;
const yourAccessToken = 'xxx';

client.setAccessToken(yourAccessToken);

let apiInstance = new platformClient.TokensApi();

apiInstance
	.getTokensMe()
	.then((data) => {
		console.log(`getTokensMe success! data: ${JSON.stringify(data, null, 2)}`);
	})
	.catch((err) => {
		console.log('There was a failure calling getTokensMe');
		console.error(err);
	});

Thank you for checking. Should the yourAccessToken be just the entire array or just the line "access_token": "9s1jsCNxpF1swHGQGmXXZhgpQTPpxII6BYAwtlNuEUxD0614nDBmMcGLs2zzTBJeJQJZbgHeZ3QviEoXXXXXX",

The response from the POST endpoint you're referring to is a JSON object. Only use the value of the access_token property.

Got it thank you.

and its working. could have sworn i tried that combo yesterday. Thank you tim.smith as always.

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.