manually adding/updating an oauth token in oauth_credential table

Davie
Giga Expert

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

 

1 ACCEPTED SOLUTION

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
}

View solution in original post

2 REPLIES 2

Anil Lande
Kilo Patron

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 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
}