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

Hello Rajeev,



How do you re-generate the Refresh Token before it expires? Doesn't it require user consent?



Thanks,


Probir


When you generate your auth token , you get refresh token and expiration date time of refresh token as well. So you can trigger event schedule to run before the expiry of your refresh token. Take the below example where I schedule a event 1200 seconds before the expiry of refresh token.


#1


      var gdt = new GlideDateTime();


    gdt.setDisplayValue(current.u_expiry_date.getDisplayValue());


    gdt.addSeconds(-1200);



if(current.u_expiry_date != '' && (current.u_expiry_date != previous.u_expiry_date))


{      


     


gs.eventQueueScheduled("RefreshToken.Trigger", current, gs.getUserID(), gs.getUserName(),gdt);      


    }    




#2


and then you can write your code under "Events" --> Script actions (which will trigger when event RefreshToken.Trigger is trigerred) to generate the access token again.


find_real_file.png


Hello,

Could you please tell me how the refreshToken function looks like in your event script?

I am trying to implement a similar thing.

Thank you !

Hello, 

Can you please explain to me a bit better how you can use this code to refresh the token/ refresh token ?

Thank you !