Smartsheet Oauth2.0 GlideOAuthStringMap

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2020 05:38 AM
Hello,
I am tying to integrate the Smartsheet application with servicenow using Oauth2.0 token. The issue i am facing is the the keyword in authorization code that the smartsheet is sending is not of access_token but of code example: https://<myinst>.com/oauth_redirect.do?code=dlhmqzuefidheafy&expires_in=599928&state=483270143
as per the "Live Coding Happy Hour" on youtube https://www.youtube.com/watch?v=6KPhLYIDchU i created a custom Script include to fetch the accesstoken response, but i am not able to the the parameters as per the video. The only function that is being called is the "preprocessAuthCode" but the arguments passed to is empty and the log shows as [object GlideOAuthStringMap], not able to find the documentation to it. Could anyone help with me parsing the parameters
below is the Script include i have written
var OAuthJSSmartSheet = Class.create();
OAuthJSSmartSheet.prototype = {
initialize: function() {
gs.info("testing init");
},
interceptRequestParameters: function(requestParamMap) {
// Add/Modify request parameters if needed
gs.info("testing interceptRequestParameters="+requestParamMap.toString());
this.preprocessAccessToken(requestParamMap);
},
parseTokenResponse: function(accessTokenResponse) {
gs.info("testing parseTokenResponse="+accessTokenResponse.toString());
try {
this.postprocessAccessToken(accessTokenResponse);
} catch (e) {
gs.info(e);
}
},
preprocessAuthCode: function(requestParamMap) {
gs.info("preprocessAuthCode=" + requestParamMap);
// gs.info("preprocessAuthCode=" + requestParamMap.getBody());
// gs.info("preprocessAuthCode=" + requestParamMap.getparameters());
// gs.info("preprocessAuthCode=" + requestParamMap.getContentType());
},
preprocessAccessToken: function(requestParamMap) {
gs.info("testing preprocessAccessToken=" + requestParamMap);
},
postprocessAccessToken: function(accessTokenResponse) {
gs.info("testing postprocessAccessToken=" + accessTokenResponse);
var contentType = accessTokenResponse.getContentType();
var contentBody = accessTokenResponse.getBody();
gs.info("testing=" + contentType);
gs.info("testing=" + contentBody);
/* if (contentType && contentType.indexOf('application/json') != -1) {
var tokenResponse = (new global.JSON()).decode(accessTokenResponse.getBody());
var paramMap = accessTokenResponse.getparameters();
for (param in tokenResponse)
paramMap.put(param, tokenResponse[param].toString());
}*/
},
type: 'OAuthJSSmartSheet'
};
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2021 02:47 PM
Ah - so you are thinking this message is coming from the Smartsheet response. If that is the case, how would I remove it from the ServiceNow Request?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2021 04:35 AM
var OAuthJSSmartSheet = Class.create();
OAuthJSSmartSheet.prototype = {
preprocessAccessToken: function (requestParamMap) {
var strKeys = '';
strKeys = '' + requestParamMap.getKeys();
if (strKeys && strKeys.indexOf('expires_in') >= 0) {
//If "expires_in" is included in the request, remove it.
requestParamMap.remove('expires_in');
}
},
type: 'OAuthJSSmartSheet'
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2021 09:30 AM
Thanks - but it doesn't look like I'm hitting any of the OAuth API Scripts other than preprocessAuthCode. I've added log statements for each method and this is the only one getting called.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2021 03:35 PM
If there is only "preprocessAuthCode" in the log, then it may have failed before requesting the "AccessToken".
If so, the request for the authorization code may contain "expires_in".
To check this, try displaying the request in the script.
If you can't find it, the "expires_in" may be included in a completely different process. Try to find it in the detailed communication log.
// This script has not been run and checked once.
// Please use it with caution for errors or missing scripts.
var OAuthJSSmartSheet = Class.create();
OAuthJSSmartSheet.prototype = {
preprocessAuthCode: function (requestParamMap) {
var strKeys = '';
strKeys = '' + requestParamMap.getKeys();
gs.info('OAuthJSSmartSheet.preprocessAuthCode : strKeys = '+strKeys);
if (strKeys && strKeys.indexOf('expires_in') >= 0) {
//If "expires_in" is included in the request, remove it.
requestParamMap.remove('expires_in');
}
},
preprocessAccessToken: function (requestParamMap) {
var strKeys = '';
strKeys = '' + requestParamMap.getKeys();
gs.info('OAuthJSSmartSheet.preprocessAccessToken : strKeys = '+strKeys);
if (strKeys && strKeys.indexOf('expires_in') >= 0) {
//If "expires_in" is included in the request, remove it.
requestParamMap.remove('expires_in');
}
},
type: 'OAuthJSSmartSheet'
}
Smartsheet OAuth Error Types
https://smartsheet-platform.github.io/api-docs/?javascript#oauth-error-types
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2021 11:41 PM
Today in Japan it's a nice Saturday holiday.
I have actually set up a connection to Smartsheet. And I got the same error as you.
The reason is that ServiceNow's "oauth_redirect.do" doesn't allow "expires_in" as a parameter.
Smartsheet will send "expires_in" with the authentication code.
I couldn't find the best way to solve this problem.
So I created a new "oauth_redirect" page to relay the data to avoid the problem.
- Create a new UI Page.
This is the page that accepts redirects from Smartsheet.
UI Page "test_oauth_redirect"
<?xml version="1.0" encoding="utf-8" ?> <j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null"> <j:set var="jvar_code" value="${RP.getParameterValue('code')}" /> <j:set var="jvar_state" value="${RP.getParameterValue('state')}" /> <j:set var="jvar_expires_in" value="${RP.getParameterValue('expires_in')}" /> <p>jvar_code=${jvar_code}</p> <p>jvar_state=${jvar_state}</p> <p>jvar_expires_in=${jvar_expires_in}</p> <a href="https://MYINSTANCE.service-now.com/oauth_redirect.do?code=${jvar_code}$[AMP]state=${jvar_state}">Click here! https://MYINSTANCE.service-now.com/oauth_redirect.do?code=${jvar_code}$[AMP]state=${jvar_state}</a> </j:jelly>
- After creating the page, register this UIPage in "sys_public.list".
- [System OAuth > Application Registries] Change the "Redirect URL" in the Smartsheet settings to "https://MYINSTANCE.service-now.com/test_oauth_redirect.do".
- Set the Smartsheet system settings to the same URL.
Make sure the settings are the same for both ServiceNow and Smartsheet. - This is the end of the configuration.
- Try to get the OAuth token.
In my environment, I was able to get the token. Success.
The OAuth API Script did not require any special processing. It is useful to have the log output in there.