- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2022 05:00 PM
Hello and Good Evening.
Hope everyone is well. Am very new to Servicenow..... created a PDI to play around and one of the things I am looking at is the ability to create/update records (incidents, requests, change orders) via webservices/REST API. I was able to test this within the PDI via REST API Explorer however, when testing this using (perl or powershell) scripts outside of the PDI, and using the default "admin/admin" userid and password combination, I am getting the "Invoke-RestMethod : The remote server returned an error: (401) Unauthorized." error. Is there anything I have missed that needs to be configured for the "admin" account in the PDI and/or network related settings that needs to be configured to allow me to make webservices/REST API call to the PDI?
Thank you very much.
br
ken
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2022 05:35 PM
I think the script you are running is from the REST API Explorer page .
The script you are using is just Code Samples,so you need to change the "admin/admin" part with your own userid and password combination.
If you don't know the password of "admin" , change it first !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2022 05:18 PM
Hi, normally I would use oauth2/ a bearer token so that the admin credentials were not being passed to the instance continuously, but basic auth should work in a PDI using the admin account and just tested this successfully using Post man. How are you passing the auth type\details? Perhaps the issue is with your message configuration.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2022 05:30 PM
Hello Sir,
thank you very much.. this is the code snippet.. which is just the sample code I am currently using that is derived from the REST API explorer generated code sample..
# 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
# 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://dev125167.service-now.com/api/now/table/incident?sysparm_fields=number&sysparm_limit=10"
# Specify HTTP method
$method = "get"
# Send HTTP request
$response = Invoke-RestMethod -Headers $headers -Method $method -Uri $uri
# Specify HTTP method
$method = "get"
# Send HTTP request
$response = Invoke-RestMethod -Headers $headers -Method $method -Uri $uri
Thank you.
Best regards,
ken
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2022 05:41 PM
Base on a Postman test the auth header looks like
I recall the white space being an issue in another thread, so may pay to check the header includes the one ' ' space.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2022 05:53 PM
thank you.... stepping through the script.. these are the values...
Key Value
--- -----
Authorization Basic YWRtaW46YWRtaW4=
Accept application/json
and $headers.authorization is 'Basic YWRtaW46YWRtaW4='
br
ken