- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-08-2022 04:32 AM - edited 11-08-2022 05:31 AM
Hi All,
I'm trying to integrate the ServiceNow instance with the Salesforce sandbox. I was able to create an Oauth2.0 profile and connected to Salesforce sandbox. I was also able to pull the Account and Contact data from Salesforce through REST message.
My requirement is to retrieve the Account and Contact data on daily basis via a scheduled script. I have created a scheduled script to connect to the Salesforce sandbox and was running perfectly until the refresh token got expired. After generating the new refresh token and changing the token value in the parameters, my script was not able to get a new access token and is unable to pull the data.
I can see in the logs: Access Token value generated but script is terminated with the error message:
*** Script: accessTokenResponse.getBody()====>{"access_token":"00D7Y0000005LTD!AQYAQIW87tBantu2RfXckolXhvDh5SRGVzjoqkeWqWDV1FrY_2DYQpaOErqzld4_olX_O3lZ2tuvYopuj3Y2m5eAFNyr950X","signature":"2Hwl44ZOEN4AKfb2j1kdKE+WVBTMqK7KUxhvjiBXcqY=","scope":"lightning visualforce cdp_query_api cdp_ingest_api custom_permissions openid cdp_segment_api cdp_profile_api content refresh_token chatbot_api wave_api web chatter_api id api eclair_api pardot_api full","instance_url":"https://xyz--servicenow.sandbox.my.salesforce.com","id":"https://test.salesforce.com/id/00D7Y0000005LTDUA2/0057Y0000085ovxQAA","token_type":"Bearer","issued_at":"1667908780848"}
*** Script: AccessToken:00D7Y0000005LTD!AQYAQIW87tBantu2RfXckolXhvDh5SRGVzjoqkeWqWDV1FrY_2DYQpaOErqzld4_olX_O3lZ2tuvYopuj3Y2m5eAFNyr950X
*** Script: AccessTokenExpiresIn:0
*** Script: RefreshToken:null
Ignore oauth entity from request. Use provider from oauth entity profile.
OAuthProblemException{error='invalid_request', description='Missing parameters: access_token', uri='null', state='null', scope='null', redirectUri='null', responseStatus=0, parameters={}}
failed to get access token from remote oauth server.
Here is the Script I wrote:
var oAuthClient = new sn_auth.GlideOAuthClient();
//Set the Sys_Id of OAuth Profile found under Application Registry
var oauth_profile_id = 'faf6a2a41bd2dd10f2ab99f0b24bcb84';
var requestor_context = 'sys_rest_message';
//Sys_id of the REST Message
var requestor_id = 'd108eae41bd2dd10f2ab99f0b24bcb8e';
var params = {
grant_type: 'refresh_token',
refresh_token: '5Aep861d3D2zpStOTnSCVu1yjqxmiuLnAphleUmmOzNqSfyCt6mc6wQAktCQg2XrUMLV4imITl26B_eu0GxUVYm',
client_id: <<cliend_id>>,
client_secret: <<client_Secret>>,
redirect_uri: "https://myinstance.service-now.com/oauth_redirect.do",
//oauth_requestor_context: requestor_id,
//oauth_requestor: requestor_context,
oauth_provider_profile: oauth_profile_id
};
var json = new global.JSON();
var text = json.encode(params);
var tokenResponse = oAuthClient.requestToken('Salesforce DEV Sandbox', text);
var token = tokenResponse.getToken();
var access_token = token.getAccessToken();
gs.log("AccessToken:" + access_token);
gs.log("AccessTokenExpiresIn:" + token.getExpiresIn());
gs.log(" RefreshToken:" + token.getRefreshToken());
var r = new sn_ws.RESTMessageV2('Salesforce Sandbox', 'Get Contacts');
// var r = new sn_ws.RESTMessageV2();
// r.setEndpoint('https://xyz--servicenow.sandbox.my.salesforce.com/services/data/v55.0/sobjects/Account/');
// r.setHttpMethod('GET');
r.setAuthenticationProfile('oauth2', oauth_profile_id);
r.setRequestorProfile(requestor_context, requestor_id);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
} catch (ex) {
var message = ex.message;
//gs.print(message);
}
The above code was worked fine until I change the refresh token value.
Can anyone suggest what is wrong with the code?
@Ankur Bawiskar Could you please find the mistake in my code?
Thanks in advanace.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2023 12:38 PM
Hi @leoferrero ,
I wrote a script to get access token using client credentials.
Steps to follow:
- Change the "Default grant type to "Client Credentials" on the application registry.
- Change the authentication type to "No Authentication" on the Outbound REST Message.
- Use the following script to retrieve the access token and execute REST method:
var oAuthClient = new sn_auth.GlideOAuthClient();
var params = {
'grant_type': 'client_credentials',
'client_id': xxxxxxxx,
'client_secret': xxxxxxxx
};
var json = new global.JSON();
var text = json.encode(params);
var tokenResponse = oAuthClient.requestToken("<OAuth Application Registry Name>", text);
var token = tokenResponse.getToken();
var accessToken = 'Bearer ' + token.getAccessToken();
var r = new sn_ws.RESTMessageV2("<Outbound REST Message Name>", "<REST Method Name>");
r.setRequestHeader('Authorization', accessToken);
r.setStringParameterNoEscape('query', query); //pass the query paramenters if any.
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
Mark my answer "Helpful" if resolved your issue.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2023 10:11 AM
Hi @Community Alums did you find a solution? I have same issue
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2023 12:38 PM
Hi @leoferrero ,
I wrote a script to get access token using client credentials.
Steps to follow:
- Change the "Default grant type to "Client Credentials" on the application registry.
- Change the authentication type to "No Authentication" on the Outbound REST Message.
- Use the following script to retrieve the access token and execute REST method:
var oAuthClient = new sn_auth.GlideOAuthClient();
var params = {
'grant_type': 'client_credentials',
'client_id': xxxxxxxx,
'client_secret': xxxxxxxx
};
var json = new global.JSON();
var text = json.encode(params);
var tokenResponse = oAuthClient.requestToken("<OAuth Application Registry Name>", text);
var token = tokenResponse.getToken();
var accessToken = 'Bearer ' + token.getAccessToken();
var r = new sn_ws.RESTMessageV2("<Outbound REST Message Name>", "<REST Method Name>");
r.setRequestHeader('Authorization', accessToken);
r.setStringParameterNoEscape('query', query); //pass the query paramenters if any.
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
Mark my answer "Helpful" if resolved your issue.