如何使用 Powershell 脚本使用 SNOW OAuth REST API 对用户进行身份验证?

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

下面的link我现在已经用来验证用户进入服务:

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

但是我无法得到结果。我得到如下输出:

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

我需要提前在 Powershell scripts.Thanks 中使用 SNOW REST API 对用户进行身份验证..

使用 OAuth 的第一部分是获取访问令牌,第二部分是利用它从 service now 实例中获取数据(我在代码中添加了注释,以便您可以找到每个部分并根据您的需要编辑变量实例):

#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