- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-04-2022 08:03 AM
Hi all
I have written a script scheduled job to trigger a script get a ew access token and locate/up the current token BUT it keep adding a new token record rather then updating the current (not expired) token), any ideas here the locate and update bit:
var tk = GlideRecord('oauth_credential');
tk.addQuery('sys_id=42b4ac4d1b9f81d82982eac0b24bcba3');
tk.query();
if (!tk) {
tk.peer = '34bcf1df1b8bc550a2b0fcc6cc4bcb85';
tk.scopes = 'Flow';
tk.type = 'access_token';
tk.expires = dgt;
tk.token_received = vid;
tk.insert();
gs.log("oauth record NOT found to update, creating new " + tk.sys_id);
}
if (tk) {
tk.peer = '34bcf1df1b8bc550a2b0fcc6cc4bcb85';
tk.scopes = 'Flow';
tk.type = 'access_token';
tk.token_hash = '';
tk.expires = dgt;
tk.token_received = vid;
tk.update();
gs.log("oauth record found to update 2, = " + tk.sys_id);
} else {
gs.log("oauth record absolutley nothing found!!!");
}
the update just seems to create a new token
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-05-2022 05:09 AM
Thanks for the message sadly it didn't work
So what i ended up doing it breaking the whole script include up into various functions and that seemed to work for me like this
function SATFlow() {
var tkn = "false";
var ap = new GlideRecord('sys_properties');
ap.addQuery('name=relay.tkn.sysid');
ap.query();
if (ap.next()) {
tkn = ap.value;
}
vardgt(tkn);
}
function vardgt(tkn) {
var tkn2 = tkn;
var dgt = new GlideDateTime();
dgt.addSeconds(85000);
gettoken(tkn2, dgt);
}
function gettoken(tkn2, dgt) {
var tkn3 = tkn2;
var dgt2 = dgt;
//lookup oauth record matching the property value, if found update the token and expiry date, if not ceate new and update property with new sysid value
var tk = GlideRecord('oauth_credential');
tk.addQuery("sys_id", tkn3);
tk.query();
if (tk.next()) {
var request = new sn_ws.RESTMessageV2('SATFlow', "POST");
var response = request.execute();
var httpResponseStatus = response.getStatusCode();
var httpResponseBody = response.getBody();
var obj = httpResponseBody;
//parse through response
var vparser = new JSONParser();
var vparsed = vparser.parse(obj);
var vid = vparsed.access_token;
var vimg = vparsed.token_type;
var vthumb = vparsed.scope;
//now update token record
if (httpResponseStatus == 200) {
gs.log("oauth record found to update", 'SATFlow');
tk.expires = dgt2;
tk.token_received = vid;
tk.update();
gs.log("oauth record updated , tk= " + tk.sys_id + " matched with tkn", 'SATFlow');
}
} else if (!tk.next()) { //no token to update found
gs.log("oauth record NOT found, creating new ", 'SATFlow');
//etc etc etc to create new Token record then loop back and update the system property value with the new sysid of the token record
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-04-2022 08:36 AM
Hi,
Update it like below:
var tk = GlideRecord('oauth_credential');
tk.addQuery('sys_id','42b4ac4d1b9f81d82982eac0b24bcba3'); //corrected this line
tk.query();
if (!tk) {
tk.peer = '34bcf1df1b8bc550a2b0fcc6cc4bcb85';
tk.scopes = 'Flow';
tk.type = 'access_token';
tk.expires = dgt;
tk.token_received = vid;
tk.insert();
gs.log("oauth record NOT found to update, creating new " + tk.sys_id);
}
if (tk) {
tk.peer = '34bcf1df1b8bc550a2b0fcc6cc4bcb85';
tk.scopes = 'Flow';
tk.type = 'access_token';
tk.token_hash = '';
tk.expires = dgt;
tk.token_received = vid;
tk.update();
gs.log("oauth record found to update 2, = " + tk.sys_id);
} else {
gs.log("oauth record absolutley nothing found!!!");
}
Thanks,
Anil Lande
Thanks
Anil Lande
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-05-2022 05:09 AM
Thanks for the message sadly it didn't work
So what i ended up doing it breaking the whole script include up into various functions and that seemed to work for me like this
function SATFlow() {
var tkn = "false";
var ap = new GlideRecord('sys_properties');
ap.addQuery('name=relay.tkn.sysid');
ap.query();
if (ap.next()) {
tkn = ap.value;
}
vardgt(tkn);
}
function vardgt(tkn) {
var tkn2 = tkn;
var dgt = new GlideDateTime();
dgt.addSeconds(85000);
gettoken(tkn2, dgt);
}
function gettoken(tkn2, dgt) {
var tkn3 = tkn2;
var dgt2 = dgt;
//lookup oauth record matching the property value, if found update the token and expiry date, if not ceate new and update property with new sysid value
var tk = GlideRecord('oauth_credential');
tk.addQuery("sys_id", tkn3);
tk.query();
if (tk.next()) {
var request = new sn_ws.RESTMessageV2('SATFlow', "POST");
var response = request.execute();
var httpResponseStatus = response.getStatusCode();
var httpResponseBody = response.getBody();
var obj = httpResponseBody;
//parse through response
var vparser = new JSONParser();
var vparsed = vparser.parse(obj);
var vid = vparsed.access_token;
var vimg = vparsed.token_type;
var vthumb = vparsed.scope;
//now update token record
if (httpResponseStatus == 200) {
gs.log("oauth record found to update", 'SATFlow');
tk.expires = dgt2;
tk.token_received = vid;
tk.update();
gs.log("oauth record updated , tk= " + tk.sys_id + " matched with tkn", 'SATFlow');
}
} else if (!tk.next()) { //no token to update found
gs.log("oauth record NOT found, creating new ", 'SATFlow');
//etc etc etc to create new Token record then loop back and update the system property value with the new sysid of the token record
}