Not able to use the Access Token generated from the Salesforce OAuth profile for the REST call

pro-test
Tera Contributor

Hello Helpers,

 

I'm doing an integration from ServiceNow to Salesforce. I was able to generate the access token from the script but was not able to use it for the REST call. I'm using the Authorization code flow for this integration. I'm using the below code to generate the access token:

var clientRequest = new sn_auth.GlideOAuthClientRequest();
clientRequest.setGrantType('refresh_token');
clientRequest.setRefreshToken('<<refresh token>>');
//GlideOAuthClient
var client = new sn_auth.GlideOAuthClient();
//GlideOAuthClientResponse
var tokenResponse = client.requestTokenByRequest('<<rest message>>', clientRequest);
//GlideOAuthToken
var token = tokenResponse.getToken();
var accessToken = token.getAccessToken()

After receiving the access token, I'm not able to use it in the script to make the REST call. I tried using multiple ways to pass access token in the header but no luck.

Can you please help to achieve the same?

 

Thank You in advance!

WR//Proneet

 

 

 

5 REPLIES 5

Tony Chatfield1
Kilo Patron

Hi, with no clear details of your script or how you have tried to consume the returned token, the community doesn't have a lot of details to assist with diagnostics.
Assuming you have checked and confirmed that that 'accessToken' is a valid token?
Then in a standard REST message I would expect something like this, although the auth type (IE "OAuth-Token") , might be integration specific.
yourMessage.setRequestHeader("OAuth-Token", accessToken);

 

Hi Tony,

 

The script that I have shared, gives me the access token. Just to cross-check, I retrieved the tokens by clicking the 'Get OAuth Token' UI action and checked in the Manage Token table and found that the access tokens are the same. So it means the token that I'm getting is a valid token.

I tried using 'yourMessage.setRequestHeader("OAuth-Token", accessToken);', but that is'nt working 😞

Amit Gujarathi
Giga Sage
Giga Sage

Hi @pro-test ,
I trust you are doing great.
Please find the below reference code for the same.

var oAuthGR = new GlideRecord('oauth_entity');
oAuthGR.addQuery('name', '<<Your_OAuth_Profile_Name>>'); // Replace with your OAuth profile name
oAuthGR.query();

if (oAuthGR.next()) {
    var oAuthEntity = sn_auth.GlideOAuthEntity.getBySysID(oAuthGR.getUniqueValue());
    var tokenResponse = oAuthEntity.getAccessTokenResponse();
    
    var accessToken;
    if (tokenResponse) {
        accessToken = tokenResponse.getAccessToken();
    }

    if (accessToken) {
        gs.info("Access token: " + accessToken);
        // You can now use the access token to make authenticated API calls
    } else {
        gs.error("Failed to obtain access token");
    }
} else {
    gs.error("OAuth entity not found");
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Hi Amit,

 

Thank you for the response. I tried the above script and got the below error: 

sys_id="49b65cfb87aaf114406c42690cbb3504" level="2" source="com.glide.ui.ServletErrorListener" message="*** Script: Failed to obtain access token: no thrown error" sys_class_name="syslog" context_map="{"_page_name":"sys.scripts.do","_system_id":"app131030.dub101.service-now.com:volvocarsdev024","_logged_in_user":"PSINHA2","_scope":"global","_user":"PSINHA2","_is_impersonating":"false","_session_id":"0E5E80FF872AF114406C42690CBB3588","_txid":"c5b694fbe3a3"}"
*** Script: Failed to obtain access token: no thrown error

Is anything I'm doing wrong?