Last active
April 21, 2022 04:37
-
-
Save danderson00/89ecf427380bf64c24856c92884d2701 to your computer and use it in GitHub Desktop.
ServiceNow Authentication
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fetch = require('node-fetch') | |
const querystring = require('querystring') | |
var data = querystring.stringify({ | |
"grant_type": "password", | |
// client_id and client_secret are from the System OAuth -> Application Registrations screen on the ServiceNow portal | |
"client_id": "", | |
"client_secret": "", | |
// username and password are valid user credentials for a user in the ServiceNow instance | |
"username": "", | |
"password": "" | |
}) | |
fetch('https://instance.service-now.com/oauth_token.do', { method: 'POST', body: data, headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }) | |
.then(response => { | |
if(response.ok) | |
return response.json() | |
throw new Error('Unable to obtain request token') | |
}) | |
.then(auth => fetch('https://instance.service-now.com/api/now/table/alm_asset?sysparm_limit=10', { method: 'GET', headers: { 'Authorization': 'Bearer ' + auth.access_token } })) | |
.then(response => { | |
if(response.ok) | |
return response.json() | |
throw new Error('Unable to obtain asset data') | |
}) | |
.then(assets => console.log(JSON.stringify(assets, null, 2))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment