The CreatorCon Call for Content is officially open! Get started here.

Script to retrieve access token and refresh token for oauth shows error

Sruthe
Tera Contributor

Below script is used to retrieve access token and refresh token for oauth.

 

var oAuthClient = new sn_auth.GlideOAuthClient();

var params = {
'grant_type': 'client_credentials',
'client_id': 'abc',
'client_secret': '123'
};

var json = new global.JSON();
var text = json.encode(params);

var tokenResponse = oAuthClient.requestToken('ABC', text);
var token = tokenResponse.getToken();

gs.print("AccessToken:" + token.getAccessToken());
gs.print("AccessTokenExpiresIn:" + token.getExpiresIn());
gs.print("RefreshToken:" + token.getRefreshToken());

But error message is shown as below
OAuthProblemException{error='invalid_request', description='Multiple client credentials cannot be specified.', uri='null', state='null', scope='null', redirectUri='null', responseStatus=0, parameters={}}
*** Script: AccessToken:null
*** Script: AccessTokenExpiresIn:0
*** Script: RefreshToken:null

1 ACCEPTED SOLUTION

Ratnakar7
Mega Sage

Hi @Sruthe ,

 

The error message "Multiple client credentials cannot be specified" suggests that the client_id and client_secret parameters are being specified twice in the request. One possible reason for this error could be that the ABC parameter being passed to the oAuthClient.requestToken() method is supposed to be the name of an OAuth configuration record in ServiceNow, and it may be including the client_id and client_secret parameters already.

To fix this issue, you can modify the params object to only include the grant_type parameter, like this:

 

var params = {
  'grant_type': 'client_credentials'
};

 

Then, update the oAuthClient.requestToken() method call to pass the client_id and client_secret parameters directly as arguments:

 

var tokenResponse = oAuthClient.requestToken('abc', '123', 'ABC', params);

 

 

Thanks,

Ratnakar

View solution in original post

1 REPLY 1

Ratnakar7
Mega Sage

Hi @Sruthe ,

 

The error message "Multiple client credentials cannot be specified" suggests that the client_id and client_secret parameters are being specified twice in the request. One possible reason for this error could be that the ABC parameter being passed to the oAuthClient.requestToken() method is supposed to be the name of an OAuth configuration record in ServiceNow, and it may be including the client_id and client_secret parameters already.

To fix this issue, you can modify the params object to only include the grant_type parameter, like this:

 

var params = {
  'grant_type': 'client_credentials'
};

 

Then, update the oAuthClient.requestToken() method call to pass the client_id and client_secret parameters directly as arguments:

 

var tokenResponse = oAuthClient.requestToken('abc', '123', 'ABC', params);

 

 

Thanks,

Ratnakar