It looks like the issue is with how you are passing authentication details. For basic authentication in the CALL REST API
function, you need to include the credentials in the headers.
Here's how you can modify your code:
import base64
import json
username = 'myusername'
password = 'mysecretpassword'
auth_string = f'{username}:{password}'
auth_base64 = base64.b64encode(auth_string.encode()).decode()
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': f'Basic {auth_base64}'
}
inputs.rest_api_method = 'GET'
inputs.rest_api_headers = json.dumps(headers)
inputs.rest_api_url = 'https://mycompany.service-now.com/api/now/table/cmdb_ci?sysparm_limit=1'
inputs.rest_api_verify = False
inputs.rest_api_body = ''
Make sure to replace myusername
and mysecretpassword
with your actual credentials. This should resolve the 401 Unauthorized error.
------------------------------
Lee Stanton
------------------------------
Original Message:
Sent: Fri August 30, 2024 07:54 PM
From: Raymond Tam
Subject: CALL REST API to ServiceNow using basic authentication error
I am trying to use the CALL REST API function to query the ServiceNow CMDB CI but suck at the basic authentication issue.
I keep getting the 401 Client Error: Unauthorized error.
Below is what I have in the "CALL REST API" function". Any suggestions?
import json
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
inputs.rest_api_method = 'GET'
inputs.rest_api_headers = json.dumps(headers)
inputs.rest_api_url = 'https://mycompany.service-now.com/api/now/table/cmdb_ci?sysparm_limit=1'
inputs.rest_api_verify = False
inputs.rest_api_body = 'auth=(myusrname, mysecretpassword)'
------------------------------
Raymond Tam
------------------------------