I am not able to make API call via powershell but able to make it via python and postman. || (401) Unauthorized.

prkpushp
Giga Contributor

I am not able to make API call via powershell. I thought it could be credential issue but the same cred works on python and postman. See error below.


PS C:\temp> .\snowint.ps1
Invoke-RestMethod : The remote server returned an error: (401) Unauthorized.
At C:\temp\snowint.ps1:34 char:13
+ $response = Invoke-RestMethod -Headers $headers -Method $method -Uri ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

 

My code:

$user = 'admin'
$pwd = 'admin'

$env = "dev127921"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo))
$headers.Add('Accept','application/json')
$uri = "https://"+ $env + ".service-now.com/api/now/table/sc_req_item?sysparm_query=active%3Dtrue&sysparm_display_value=number%3DRITM0000001&sysparm_limit=1"
$method = "get"
$response = Invoke-RestMethod -Headers $headers -Method $method -Uri $uri
$response.RawContent

1 ACCEPTED SOLUTION

prkpushp
Giga Contributor

Gotcha. I was just using another variable for password. Resolved now

View solution in original post

1 REPLY 1

prkpushp
Giga Contributor

Gotcha. I was just using another variable for password. Resolved now