Suganthan Raj
Suganthan Raj

Reputation: 2424

How to Authenticate User using SNOW OAuth REST API using powershell script?

The below link i have used to authenticate the user into the service now:

https://<instance>.-now.com/oauth_token?client_id=<client_key>&client_secret=<client_secret>&username=admin&password=admin

But i can't able to get the results. I have got the output as below:

{
   "error_description": "access_denied",
   "error": "server_error"
 }

I need to authenticate the user using SNOW REST API in Powershell scripts.Thanks in advance..

Upvotes: 2

Views: 6054

Answers (1)

ClumsyPuffin
ClumsyPuffin

Reputation: 4059

The First part for using OAuth is getting the access token and second part is to utilize it for fetching data from service now instance (I have commented in the code so that you can find each part and edit variables as per your instance):

#For getting access token you can use this code:


$username = “admin”

$password = 'Zen123$$'

$ClientID = “62a88515890332007975e610a3216c62”

$ClientSecret = “pass@word1”


$RestEndpoint = ‘https://myinstance.service-now.com/oauth_token.do’

$body = [System.Text.Encoding]::UTF8.GetBytes(‘grant_type=password&username=’+$username+’&password=’+$password+’&client_id=’+$ClientID+’&client_secret=’+$ClientSecret)

$result = Invoke-RestMethod -Uri $RestEndpoint -Body $Body -ContentType ‘application/x-www-form-urlencoded’ -Method Post

$access_token = $result.access_token


#Once you have access token, you can utilize the invoke-restmethod to fetch data from servicenow : 

$URI = ‘https://myinstance.service-now.com/api/now/table/incident?sysparm_limit=1’
$headers = @{“authorization” = “Bearer $access_token”}

$result = Invoke-RestMethod -Uri $URI -Headers $headers
$result 

Upvotes: 2

Related Questions