Hi , i need to enter password from the internal vault which rotates on daily basis due to that i can’t add user id and password into headers as Authorization , Basic …
is there any way i can skip this and add user id and password into the code as credentionls
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Basic asdfsfsdfsdf==")
$headers.Add("Content-Type", "application/json")
$body = "{ 'type':'New Change Request','incident':'INC1111'}"
$response = Invoke-RestMethod 'https://server1.service-now.com/api/server/auto1' -Method 'POST' -Headers $headers -Body $body
$response | ConvertTo-Json
AFAIK, no. I have never seen an API which accepts username and password. There are cases were username and password is converted to a specific pattern then converted to base64 to be used in headers,
i tried but unable to check how to use this in the rest api call headers. is the something i am missing
$base64AuthInfo = [convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1} -f $user, $pass")))
$base64AuthInfo
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Basic $base64AuthInfo")
you need to check the API documentation for the endpoint you are trying.
From the initial post, it looks like servicenow, and accepts Bearer token. And to get the Bearer token you need couple more informations like clientID, client secret and password.
See more here: How to generate bearer token for oAuth 2.0 - Authorization Grant type - Support and Troubleshooting (servicenow.com)
it worked now
#caputre user id and password from the cyberark URL
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$pa = Invoke-WebRequest 'https://server' | ConvertFrom-Json
#save username and password from the $pa variable
$SnowUserName=$pa.UserName
$SnowPassword=$pa.Content
#snow base url to post the CR
$SnowAPIURL='https://service-now.com/api/<>'
#convert snowusername and snowpassword into base64 auth for authentication
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $SnowUserName,$SnowPassword)))
$Snowheaders = @{Authorization=("Basic {0}" -f $base64AuthInfo)}
$Snowheaders.Add("Content-Type", "application/json")
#body part to open a change requqest on the basis on incident ID received from workflow
$body = "{'type':'New Change Request','incident':'123'}"
#post the change request on the basis of incident recieved.
$response = Invoke-RestMethod -Uri $SnowAPIURL -Method 'POST' -Headers $Snowheaders -Body $body
$response | ConvertTo-Json
$out=$response.result.change_request
$out