How can we get the access Token for OAuth2.0 as grant type of client credentials

ashokbodaga
Tera Contributor

Hi ,

 

I have a requirement like I created a OAuth provider as a Grant type of "Client credentials". How can we get the Access token and refresh token  for every 15 minutes to make the connection active. I had referred few posts as a grant type of Resource owner password credentials.

 

Can some one help me how can I achieve this.

 

Thank you.

2 REPLIES 2

-O-
Kilo Patron
Kilo Patron

Have you looked into the GlideOAuthToken API?

E.g, if you have defined an OAuth 2.0 Credentials [oauth_2_0_credentials] record for an OAuth client (with a grant type of Client Credentials), you could write something like:

var oAuthClient = new sn_auth.GlideOAuthClient();
var oauth20Credentials = '<Sys ID of the OAuth 2.0 Credentials [oauth_2_0_credentials] record>';
var oauthEntityProfile = 'value of field OAuth Entity Profile [oauth_entity_profile] of the same OAuth 2.0 Credentials [oauth_2_0_credentials] record';
var token = oAuthClient.getToken(oauth20Credentials, oauthEntityProfile);

gs.debug(token.getAccessToken());

 

Ankur Bawiskar
Tera Patron
Tera Patron

@ashokbodaga 

refer below links

OAuth2.0: Get new Access Token from existing Refresh Token

How to Setup OAuth2 authentication for outbound RESTMessageV2 integrations

OAuth : Script to Automate Token Request

How to generate the access token once it is expired from Business Rule?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader