Not receiving OAuth Refresh Token

Kumar122
Tera Contributor

When we manually renew the "Access Token" I am only getting the "Access Token" renewed. My understanding is that both "Access Token", "Refresh Token" are to be renewed. Because of not having "Refresh Token" we have to manually renew "Access Token" as the expiry time on these "Access Token" are short (1 hr. only).

 

When discussed this with Azure team, they are saying that we don't need to configure anything in ServiceNow "Application Registries" for the field "Refresh Token URL"

 

Appreciate any help with this one. Thank you!

 

Regards
Kumar

8 REPLIES 8

Sateesh Kumar D
ServiceNow Employee
ServiceNow Employee

Hello,

Make sure to set "Clock Skew" to match Azure token lifetime.

Hi @Sateesh Kumar D 

 

Not sure how to verify if the "Clock Skew" matches to Azure token lifetime. In my case, the Azure is providing 1 hr. valid "Access Token" but no "Refresh Token". Thank you!

 

Regards

Kumar

Amit Pandey
Kilo Sage

Hi @Kumar122 

 

Can you help me with the Authorization Code you're using. Would recommend you to use client credentials. You can test it with following BR-

 

try {
    var client_id = 'Enter your client id';
    var client_secret = 'Enter your client secret';
    var grant_type = 'client_credentials';
    var token_endpoint = 'https://example.com/services/oauth2/token';
    var token_grant_url = token_endpoint + "?grant_type=" + grant_type + "&client_id=" + client_id + "&client_secret=" + client_secret;

    var token_request = new sn_ws.RESTMessageV2();
    token_request.setEndpoint(token_grant_url);
    token_request.setHttpMethod("post");

    var token_response = token_request.execute();
    var response_body = token_response.getBody();
    var response_obj = JSON.parse(response_body);
    var access_token = response_obj.access_token;

    gs.info('Token request response: ' + response_body);
    gs.info('Access token: ' + access_token);

    // Use the access token to make API calls
    var api_request = new sn_ws.RESTMessageV2();
    api_request.setEndpoint('https://example.com/services/data/v58.0/sobjects/Ticket__c');
    api_request.setHttpMethod('post');
    api_request.setRequestHeader('Authorization', 'Bearer ' + access_token);
    api_request.setRequestHeader('Accept', 'application/json');
    api_request.setRequestHeader('Content-Type', 'application/json');

    var restObj = {
        "ServiceNow_SysID__c": current.sys_id + '',
        "Category__c": current.category + '',
        "Subcategory__c": current.subcategory + '',
        "Number__c": current.number + '',
        "Opened_by__c": current.opened_by.name + '',
        "State__c": current.state.getDisplayValue() + '',
        "Assigned_to__c": current.assigned_to.name + '',
        "Channel__c": current.contact_type.getDisplayValue() + '',
        "Short_Description__c": current.short_description.toString().replace(/\v|\r|\0/g, "") + '',
        "Description__c": current.description.toString().replace(/\v|\r|\0/g, "") + '',
        "Name": current.number + ''
    };
    var restStr = JSON.stringify(restObj);

    api_request.setRequestBody(restStr);

    gs.info('API Request Endpoint: ' + api_request.getEndpoint());
    gs.info('API Request Headers: ' + api_request.getRequestHeaders());

    var api_response = api_request.execute();
    var api_response_body = api_response.getBody();

    gs.info('API Response Object: ' + api_response);
    gs.info('API Response Body: ' + api_response_body);
    gs.info(current.number + "\nSalesforce Case Create : HTTP Status : " + api_response.getStatusCode() + "\nRequest Body : " + api_request.getRequestBody() + "\nResponse Body : " + api_response_body);
} catch (error) {
    gs.error('An error occurred: ' + error);
}

 

Please mark my answer helpful and correct.

 

Regards,

Amit

 

 

funkeke
Kilo Sage

ServiceNow is supposed to generate the refresh token automatically for authorization code grant, however it won't do this unless you include the "offline_access" scope on your OAuth registry and profile. Without that scope it will generate just the access token. You can see tokens generated in oauth_credential.list (add Type to the view).

 

More on the "offline_access" convention.