Rest API integration for bearer token

Arun87
Giga Contributor

Hi,

  We need to integrate servicenow with third party tool. Third party tool generated bearer token and could you please help us how to add the bearer token in servicenow.

In servicenow we have created the catalog item along with the workflow. In workflow we have added run script to call the rest message. 

Could any one please  help us to add the bearer token in servicenow.

4 REPLIES 4

Tony Chatfield1
Kilo Patron

Hi, your description does not clarify your configuration or how you intend to use the token.
Normally you would create an Application Registry entry and your token(s) would be maintained in 'oauth_credential' table.

I would suggest that you start by reviewing the ServiceNow REST documentation and work through the REST learning material available in the developer portal.

 

Does this apply if you are trying to connect to a third-party API from ServiceNow? I use a service from VMware which has a REST API that requires an authorization flow using a username/password (Client ID/Client Secret) to retrieve a refresh_token that is good for 3 months. That refresh_token is used by itself to request a bearer_token that is valid for 8 hours of continuous use, but times out after 20 minutes of unuse.

 

I'm trying to figure out how to store that refresh token and/or bearer token along with it's expiration time and be able to call that in a JavaScript code for a workflow as part of a catalog item request

Tokens retrieved by your instance via an 'Application registry' entry should be automatically stored in OAuth credentials table 'oauth_credential.list'

System OAuth > Manage Tokens.
ServiceNow runs a clean up job that purges expired records but your refresh token should be found here, while it is valid and can be consumed\used in script via a lookup.

Dennis R
Tera Guru

I know this is old, but I don't see a good answer to this yet so I thought I'd chime in with an answer since it shows up in Google searches.

 

If you're scripting the API call, I would avoid hard-coding the bearer token in your script. You can use something like this:

 

 

var bearerToken = "YourBearerTokenGoesHere"; // Or better, fetch it from a property
var request = new sn_ws.RESTMessageV2("REST Message", "REST Method");
request.setRequestHeader("Authorization", "BEARER " + bearerToken);
var response = request.execute();

 

 

Or if you haven't set up an outbound REST message or just want to script the whole thing manually, something like this:

 

 

var bearerToken = "YourBearerTokenGoesHere"; // Or better, fetch it from a property
var midServerName = "Your MID Server Name"; // If applicable
var request = new sn_ws.RESTMessageV2();

request.setEndpoint("https://your-endpoint.com/api/whatever");
request.setHttpMethod("GET"); // Or POST, or other method
request.setMIDServer(midServerName); // If applicable
request.setQueryParameter("Content-Type", "application/json"); // If applicable
request.setQueryParameter("Authorization", "BEARER" + bearerToken);
var response = request.execute();