How to get refresh token for Google Oauth API Calls
Import
·
Jun 30, 2020
·
article
Ok, so you've followed this article on how to do a outbound REST call to Google with OAuth
...but now what? How do you refresh your OAuth token via script to make the API calls again? You're going to need a "Refresh Token" but no one says how to get one...until now
(Assuming you are using Client ID and Client Secret for authentication)
- Go to the Google OAuth Playground: https://developers.google.com/oauthplayground/
- Click the Gear Icon in the upper right of the page.
- 1. Click Use your own OAuth credentials checkbox
- Enter the OAuth Client ID and OAuth Client secret you are using in your OAuth Application Registry [oauth_entity]
- Click Close
- Enter the OAuth Client ID and OAuth Client secret you are using in your OAuth Application Registry [oauth_entity]
- In Step 1 Select & authorize APIs, there is a input field to Input your own scopes. Fill this with the your OAuth Scope from the OAuth Entity Scopes related list on your Application Registry
- Click the Authorize APIs button.
- You are likely to get a popup with Error 400: redirect_uri_mismatch
- In the popups, click the account and Allow the access to the google account
- The OAuth Playground should now move you to Step 2 Exchange authorization code for tokens with the Authorization code filled in
- Click the Exchange authorization code for tokens button
- The Request / Response will populate in the right pane. Save this information! In here is your refresh_token.
- Now you can script a call to refresh your OAuth access token using tour refresh token
- 1.
//refresh_token received from previous steps var refreshToken = "1/########################-########################-########################-########################"; //Application Registry NAME var applicationRegistry = "Google"; // 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(applicationRegistry, clientRequest); // GlideOAuthToken var token = tokenResponse.getToken(); var expiresIn = token.getExpiresIn(); var accessToken = token.getAccessToken();
2. Might be a good idea to store your refresh token in a system property 13. Once you have things working, be sure to have your google administrator remove the redirect URI authorization to the playground.
References:
Unable to refresh access token : response is “unauthorized_client”
View original source
https://www.servicenow.com/community/now-platform-articles/how-to-get-refresh-token-for-google-oauth-api-calls/ta-p/2321248
