Refreshing OAuth for Google Cloud Using Script?

aashishdasari
Tera Expert

First, I have created a OAuth Profile for Google Cloud in the Application Registry using the 'Connect to a third party OAuth Profile'. The Profile created is as follows.

find_real_file.png

find_real_file.png

find_real_file.png

I am using the OAuth Profile in the REST Messages to Various Calls such as Create VM,Turn Off Vm,Turn On VM etc., when I am using the OAuth Profile it shows the following message every time that 'OAuth Access or Refresh tokens are not available. Verify the OAuth configuration and click the 'Get OAuth Token' link below to request a new token.'

find_real_file.png

find_real_file.png

Everytime I query a REST message it shows this and I need to Click on Get OAuth Token to get the Token. The Validity of the token lasts for only an hour after that I need to refresh it again by clicking on the Get OAuth Token.

find_real_file.png

I need to know is there any way by Scripting or any other method to get the OAuth Token without Clicking the Get OAuth Token and also token lasts for a longer time.

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

Hi Aashish,



The key is you need to get an 'offline' token so it lasts as long as your refresh token lifespan indicates.



josh.nerius covered this on the 9-23-2016 edition of Live Coding Happy Hour. I had to implement it myself recently.



Watch this video. He starts somewhere around 40:00.



ServiceNow Live Coding Happy Hour for 2016-09-30 - OAuth Part 2 - YouTube


View solution in original post

16 REPLIES 16

Chuck Tomasi
Tera Patron

Hi Aashish,



The key is you need to get an 'offline' token so it lasts as long as your refresh token lifespan indicates.



josh.nerius covered this on the 9-23-2016 edition of Live Coding Happy Hour. I had to implement it myself recently.



Watch this video. He starts somewhere around 40:00.



ServiceNow Live Coding Happy Hour for 2016-09-30 - OAuth Part 2 - YouTube


Hello!

 

I am getting the same behavior whilst setting up the Cornerstone Spoke. Following ServiceNow docs on setting up the Cornerstone spoke, there is no mention of getting an 'offline' token. Any tips on this would be much appreciated. 

Screenshot 2023-05-03 at 1.34.57 PM.png

 

-Rod

rajeevlochan82
Mega Guru

I do not think he code automatically Generated or Refreshes O-Auth Token. You probably have to do that Programatically.   I did couple of O-Auth based Integrations and created self code for generating Access Tokens. I created First Access Token and Refresh Token and Parsed other useful values like Token Expiry Time Span e.t.c. Then I use to run a event / scheduled job (which use to run just few minutes before the token expiry time so that I can get new set of Access/ Refresh token)



// Code for BOX Integration using O-Auth to generate and parse access token / refersh token and other useful information



var getToken = new RESTMessage('Get BOX Token', 'post');


  getToken.setStringParameter('client_id',   current.u_endpoint.u_client_id);


  getToken.setStringParameter('client_secret', current.u_endpoint.u_client_secret);


  getToken.setStringParameter('authcode', current.u_token);


  getToken.setStringParameter('grant_type', 'authorization_code');


  getToken.setStringParameter('TokenType', 'code');


  var response = getToken.execute();


  var output = response.getBody();


  var parsedData = JSON.parse(response.getBody())



  if(typeof parsedData.access_token != "undefined")


  {


  current.u_access_token = parsedData.access_token;


  }



  if(typeof parsedData.expires_in != "undefined")


  {


  current.u_expires_in = parsedData.expires_in.toString();


  }



  if(typeof parsedData.refresh_token != "undefined")


  {



  current.u_refresh_token = parsedData.refresh_token;


  }



  if(typeof parsedData.token_type != "undefined")


  {


  current.u_token_type = parsedData.token_type


  }



  if(output.indexOf('error') >-1)


  {


  // add to notification


  }



  if(typeof parsedData.expires_in != "undefined")


  {


  var expDate = new GlideDateTime();


  var tz = gs.getSession().getTimeZone();


  expDate.setTZ(tz);


  expDate.setDisplayValue(gs.nowDateTime())


  expDate.addSeconds(parsedData.expires_in);


  current.u_expiry_date = expDate.getDisplayValue();


  current.update();


  }



  current.u_response = output;


  current.update();


  }


Hi Rajeev,



Can you please provide the code to automate getting 'access token' instead of clicking 'Get OAuth Token' each time the access token expires.



Thanks,


Sai