Page 1 of 1

Can't get HTTPRequest Authorization working

Posted: Wed Jul 28, 2021 2:17 am
by Steving
Hello all,

I hope someone can help me with this. I have done this exact thing before with another site and it's working fine, but for some reason I'm trying to develop something with ServiceNOW and I can't get the authorization working. I've tried various ways of doing it but every time I try and post something the message comes back that I'm not authorized. Here is the code that I'm using:

Code: Select all

Define.s UserName, Password, EncodedLogin, TicketString, FullResponse, ResponseCode, URLString
Define *Login, Running

If Not InitNetwork()
  Debug "Problem initializing the network! Program Ending"
  End 101
EndIf

NewMap Header.s()

Username = "admin"
Password = "<mypassword>"
*Login = UTF8(UserName + ":" + Password)
EncodedLogin = Base64Encoder(*Login, MemorySize(*Login))

Header("Authorization") = "Basic " + EncodedLogin
Header("Accept") = "application/xml"
Header("Content-Type") = "application/xml"
ForEach Header()
  Debug MapKey(Header()) + ": " + Header()
Next
Debug ""
Debug "---------------------------------------------------------------"
Debug ""
URLString = "https://mydevelopmentinstance.service-now.com/api/now/table/sys_user"

Running = HTTPRequest(#PB_HTTP_Get, URLString, "", 0, Header())
If Running
  FullResponse = HTTPInfo(Running, #PB_HTTP_Response)
  ResponseCode = HTTPInfo(Running, #PB_HTTP_StatusCode)
  LogSomething(#TAB$ + "Got " + FullResponse)
  FinishHTTP(Running)
  Debug "Running: " + Running
  Debug "Response Code: " + ResponseCode
  Debug "Response: " + FullResponse
EndIf
What I get back from them is
<detail>Required to provide Auth information</detail><message>User Not Authenticated</message>
I thought maybe PureBasic already put in the BASIC for me on the authorization so I took that out, did many other things and nothing seems to allow me to authenticate. I was able to get this to work in PowerShell and it works:

Code: Select all

# Eg. User name="admin", Password="admin" for this code sample.
$user = "admin"
$pass = "mypassword"

# 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://mydevelopmentinstance.service-now.com/api/now/table/sys_user?sysparm_limit=10"

# Specify HTTP method
$method = "get"

# Send HTTP request
$response = Invoke-RestMethod -Headers $headers -Method $method -Uri $uri 

# Print response
$response
Therefore, I know that I'm not totally wrong about this and that it CAN work. I hope that there is someone out there that has encountered this before and perhaps even dealt with the ServiceNow API and can help point me in the right direction.

Thanks so much,

stevinga

Re: Can't get HTTPRequest Authorization working

Posted: Wed Jul 28, 2021 6:57 am
by infratec
If you inspect what you are sending

Code: Select all

*Login = UTF8(UserName + ":" + Password)

ShowMemoryViewer(*Login, MemorySize(*Login))
You see that there is an additional terminating 0 at the end.

Try this:

Code: Select all

EncodedLogin = Base64Encoder(*Login, MemorySize(*Login) - 1)

Re: Can't get HTTPRequest Authorization working

Posted: Wed Jul 28, 2021 6:04 pm
by Steving
Thanks so much! That did the trick! :D :D :D