Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to update the access token from oauth credential table

Obito
Tera Expert

Hi all,

 

I have fetched the access token using a script and I want to update the access token with a new one inside oauth credential table. How I can do it?

 

Thanks 

1 ACCEPTED SOLUTION

Mohan raj
Mega Sage

Hi @Obito,

 

Access token are stored in oauth_credential to insert the access token manually try below script it would helpful

 

var response = { "access_token": "ya29.c.b0Aaekm1L2B_W-8A3-Vqfo18k4dTMGihI4EK9KmXgR60q9sVzUMMbc5YWe2k-S3HsPkzLoUAfi2RTblkq-R8k_FAYqpvvQ0rW43mUK86mZpXEZGl5RBwDdqrE-DURhubsKTLdY-d7Fnc-1HTT_ZSOchagZPa5tNTXzQCu8UcFNR5sQqHJyka-D1w-SNcB8XiyKIuUaqZU9SSigbfoSWoy3dKc5vSjLHc2qmdTXYxAzBI1vf8wsD", 
"expires_in": 3599, 
"token_type": "Bearer" };  //Example response



var oauth = new GlideRecord('oauth_credential');
oauth.initialize();
oauth.peer.name = 'storing_accesstoken';
oauth.token_received = response.access_token;
var gdt = new GlideDateTime();
gdt.addSeconds(response.expires_in);
oauth.expires = gdt.getDisplayValue();
oauth.insert();

 

If my response helps you to resolve the issue close the question by Accepting solution and hit thumb icon. From Correct answers others will get benefited in future.

Regards,

T Mohan.

View solution in original post

1 REPLY 1

Mohan raj
Mega Sage

Hi @Obito,

 

Access token are stored in oauth_credential to insert the access token manually try below script it would helpful

 

var response = { "access_token": "ya29.c.b0Aaekm1L2B_W-8A3-Vqfo18k4dTMGihI4EK9KmXgR60q9sVzUMMbc5YWe2k-S3HsPkzLoUAfi2RTblkq-R8k_FAYqpvvQ0rW43mUK86mZpXEZGl5RBwDdqrE-DURhubsKTLdY-d7Fnc-1HTT_ZSOchagZPa5tNTXzQCu8UcFNR5sQqHJyka-D1w-SNcB8XiyKIuUaqZU9SSigbfoSWoy3dKc5vSjLHc2qmdTXYxAzBI1vf8wsD", 
"expires_in": 3599, 
"token_type": "Bearer" };  //Example response



var oauth = new GlideRecord('oauth_credential');
oauth.initialize();
oauth.peer.name = 'storing_accesstoken';
oauth.token_received = response.access_token;
var gdt = new GlideDateTime();
gdt.addSeconds(response.expires_in);
oauth.expires = gdt.getDisplayValue();
oauth.insert();

 

If my response helps you to resolve the issue close the question by Accepting solution and hit thumb icon. From Correct answers others will get benefited in future.

Regards,

T Mohan.