Invoke-RestMethod : The remote server returned an error: (401) Unauthorized.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-18-2019 02:43 PM
Hi Community,
I'm trying to utilize REST API to do some automations.
My code is working well on my developer instance:
# Eg. User name="admin", Password="admin" for this code sample. $user = "admin" $pass = "admin" # Build auth header
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))
# Set proper headers
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo))
$headers.Add('Accept','application/json')
# Specify endpoint uri
$uri = "https://downeritadtl.service-now.com/api/now/table/incident?sysparm_query=number%3DINC0222772&sysparm_limit=1"
# Specify HTTP method
$method = "get"
[Net.ServicePointManager]::SecurityProtocol = "tls12, tls11, tls"
# Send HTTP request
$response = Invoke-RestMethod -Headers $headers -Method $method -Uri $uri
# Print response
$response.Result
I tried it on a client instance without admin role and its working fine as well.
However I get the following issue on another instance:
Invoke-RestMethod : The remote server returned an error: (401) Unauthorized.
At line:23 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
The user that is using the instance that is getting the error already has an admin role.
What could be causing the 401?
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-20-2019 05:27 PM
401 for GET almost always means bad username or bad password.
If you are blocked by an ACL the status will 200 with no data.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-20-2020 01:50 PM
Hi,
I am not expecting you still have this issue but just to prevent other people from wasting the time.
If your password is strong enough and contain some chars like $ you must use single quotes.
$user = 'admin'
$pass = 'admin'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-18-2020 02:13 AM
I am also getting same error
Invoke-RestMethod : The remote server returned an error: (401) Unauthorized.
Please help here