About loginClientCredentialsGrant

Hi,

I have been trying to use a modification of the get/api/v2/conversations/calls API where the code as follows:

const platformClient = require('purecloud-platform-client-v2');
const client = platformClient.ApiClient.instance;
client.setEnvironment(platformClient.PureCloudRegionHosts.ap_southeast_2);
const CLIENT_ID = " ";
const CLIENT_SECRET = " ";

let organizationApi = new platformClient.OrganizationApi();

//Authenticate with Genesys Cloud
client.loginClientCredentialsGrant(CLIENT_ID, CLIENT_SECRET)
    .then(() => {
        organizationApi.getOrganizationsMe()
        .then((data) => {
            // console.log(`getOrganizationsMe success! data: ${JSON.stringify(data, null, 2)}`);
            client.setAccessToken(data);
        })
        .catch((err) => {
            console.log('There was a failure calling getOrganizationsMe');
            console.error(err);
        });
    }).catch((err) => {
         console.log('There was a failure calling loginClientCredentialsGrant');
         console.error(err);
    });

let apiInstance = new platformClient.ConversationsApi();

// Get active call conversations for the logged in user
apiInstance.getConversationsCalls()
  .then((data) => {
    console.log(`getConversationsCalls success! data: ${JSON.stringify(data, null, 2)}`);
  })
  .catch((err) => {
    console.log("There was a failure calling getConversationsCalls");
    console.error(err);
  });

I have had an issue set auth token bit where I would use client.loginClientCredentialsGrant(CLIENT_ID, CLIENT_SECRET) and then run the apiInstance.getConversationsCalls(). This will return a 401 with a message "No authentication bearer token specified in authorization header". The developer guide has stated in the API explorer

// Manually set auth token or use loginImplicitGrant(...) or loginClientCredentialsGrant(...)
client.setAccessToken("your_access_token");

Not sure what I've done wrong in setting the auth token. Any help is appreciated. Thanks.

Hello,

You do not need to set the access token when you invoke loginClientCredentialsGrant.
You can manually set the token (if you obtained it from somewhere else) with the setAccessToken OR use loginImplicitGrant/loginImplicitGrant.

Regarding your code (using the setAccessToken), there are in fact 2 errors (but you can simply remove the setAccessToken as I just wrote above):
You are using the result of the getOrganizationsMe to get the token. It is not correct. This contains the result of the getOrganizationsMe query.
The token is propagated back as result of the loginClientCredentialsGrant.
And it is available in an accessToken property/attribute.

client.loginClientCredentialsGrant(CLIENT_ID, CLIENT_SECRET)
    .then((authData) => {
        client.setAccessToken(authData.accessToken);
        organizationApi.getOrganizationsMe()
           .then((data) => {
               // console.log(`getOrganizationsMe success! data: ${JSON.stringify(data, null, 2)}`);
           })

or just (no setAccessToken)

client.loginClientCredentialsGrant(CLIENT_ID, CLIENT_SECRET)
    .then(() => {
        organizationApi.getOrganizationsMe()
           .then((data) => {
               // console.log(`getOrganizationsMe success! data: ${JSON.stringify(data, null, 2)}`);
           })

Regards,

1 Like

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