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

Rovan1
Tera Contributor

did you have to do anything to update the Refresh Token when they expired @Amruta12  ?

 

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();

Robert, this indeed seems the correct way to go, thank you.

Can you provide some guidance on how you obtained the refresh token?

Never mind. I think i got it from the playground after authenticating to the playground with my service account

For future reference, I have documented steps to get refresh token here: https://community.servicenow.com/community?id=community_create_article