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

标签 rest powershell servicenow

我现在使用以下链接对用户进行身份验证:

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 脚本中使用 SNOW REST API 对用户进行身份验证。提前致谢..

最佳答案

使用 OAuth 的第一部分是获取访问 token ,第二部分是利用它从服务实例中获取数据(我已在代码中进行了注释,以便您可以找到每个部分并根据您的实例编辑变量) :

#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 

关于rest - 如何使用 powershell 脚本使用 SNOW OAuth REST API 对用户进行身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44240898/

相关文章:

android - 包含来自 REST 服务的项目的 ListView

用于 Tomcat 的 REST Web 服务

Spring MVC Spring 安全性和错误处理

arrays - Powershell连接路径返回数组

java - 在 Spring 框架中使用 WebClient 进行 REST 调用时出错

powershell - 如何在 GitLab CI YAML 文件中包含 PowerShell 脚本文件

powershell - 未获取所需状态配置凭据私钥

servicenow - 现在无法获取服务中的总结果数

ServiceNow:从业务规则启动异步脚本

c# - ServiceNow - 获取所有记录