Refresh oauth token for outbound rest call

Jon Ulrich
Kilo Guru

I have an outbound rest API integration with google g suite. The authentication uses an "Authorization Code" OAuth Profile. I am looking for help on how to pragmatically refresh the OAuth token before running the outbound REST call.

1 ACCEPTED SOLUTION

Robert Beeman
Kilo Sage

Hi Jon,

We did something similar with G Suite. We generated a refresh token from Google OAuth 2.0 Playground, stored it as a system property (refreshToken variable below), and then used this code to obtain a new Access token when needed. 'G Suite Admin SDK' being the name of the OAuth Provider in the Application Registries.

// GlideOAuthClientRequest
var clientRequest = new sn_auth.GlideOAuthClientRequest();
clientRequest.setGrantType('refresh_token');
clientRequest.setRefreshToken(refreshToken);

// GlideOAuthClient
var client = new sn_auth.GlideOAuthClient();

// GlideOAuthClientResponse
var tokenResponse = client.requestTokenByRequest('G Suite Admin SDK', clientRequest);

// GlideOAuthToken
var token = tokenResponse.getToken();
var expiresIn = token.getExpiresIn();
var accessToken = token.getAccessToken();

View solution in original post

10 REPLIES 10

None of these explain how to use an "Authorization Code" OAuth Profile, but a username/password profile

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

to be on safer side it is recommended generating the OAuth token before consuming actual endpoint first to avoid the expiration time of access token

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.

Regards
Ankur

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

Hi Ankur

In Paris version, I observed that when we execute REST Message with OAUTH authentication, it automatically updates the token (checking under Manage Tokens). This works without writing code for checking if token expired and then getting refresh token. So is having REST Message with OAUTH profile association good enough?

I did not find any documentation around it.

Thank you