I am using getTokenTemp Function to get Tocken Information based on ClientID, Secret and I am getting 401: Unauthorized error. Same URL/Client/Secret Information along with same header, if I put it in POSTMAN Client, I am getting Token Information successfully. What am I doing wrong here ? I am using MS Access VBA
<code> Public Function getTokentemp() As String
Dim url As String
Dim Auth As String
Dim headers As Object
Dim params As Object
Dim tokenRequest As Object
clientId = "CLIENTID GOES HERE"
clientSecret = "SECRET INFO HERE"
url = "https://URLHERE.com/oauth/token"
Auth = clientId & ":" & clientSecret
'MsgBox (Auth)
'MsgBox (url)
' Encode Auth string to Base64
Auth = EncodeBase64(Auth)
' Create headers dictionary
Set headers = CreateObject("Scripting.Dictionary")
headers("Authorization") = "Basic " & Auth
headers("Content-Type") = "application/json"
' Create parameters dictionary
Set params = CreateObject("Scripting.Dictionary")
params("grant_type") = "client_credentials"
params("x-sap-sac-custom-auth") = "true"
' Create HTTP request object
Set tokenRequest = CreateObject("MSXML2.ServerXMLHTTP")
With tokenRequest
.Open "POST", url, False
' Set request headers
For Each Key In headers
.setRequestHeader Key, headers(Key)
Next Key
' Send request with parameters
.send Join(params.Items, "&")
' Check response status code
If .Status = 200 Then
getTokentemp = .responseText
MsgBox getTokentemp
Else
' Display error message
MsgBox "Error: " & .Status & " - " & .StatusText
End If
End With
' Clean up
Set tokenRequest = Nothing
End Function
Function EncodeBase64(ByVal str As String) As String
Dim arrData() As Byte
Dim objXML As Object
' Convert string to byte array
arrData = StrConv(str, vbFromUnicode)
' Encode byte array to Base64
Set objXML = CreateObject("MSXML2.DOMDocument")
With objXML.CreateElement("b64")
.DataType = "bin.base64"
.NodeTypedValue = arrData
EncodeBase64 = .text
End With
Set objXML = Nothing
End Function
</code>
<code> Public Function getTokentemp() As String
Dim url As String
Dim Auth As String
Dim headers As Object
Dim params As Object
Dim tokenRequest As Object
clientId = "CLIENTID GOES HERE"
clientSecret = "SECRET INFO HERE"
url = "https://URLHERE.com/oauth/token"
Auth = clientId & ":" & clientSecret
'MsgBox (Auth)
'MsgBox (url)
' Encode Auth string to Base64
Auth = EncodeBase64(Auth)
' Create headers dictionary
Set headers = CreateObject("Scripting.Dictionary")
headers("Authorization") = "Basic " & Auth
headers("Content-Type") = "application/json"
' Create parameters dictionary
Set params = CreateObject("Scripting.Dictionary")
params("grant_type") = "client_credentials"
params("x-sap-sac-custom-auth") = "true"
' Create HTTP request object
Set tokenRequest = CreateObject("MSXML2.ServerXMLHTTP")
With tokenRequest
.Open "POST", url, False
' Set request headers
For Each Key In headers
.setRequestHeader Key, headers(Key)
Next Key
' Send request with parameters
.send Join(params.Items, "&")
' Check response status code
If .Status = 200 Then
getTokentemp = .responseText
MsgBox getTokentemp
Else
' Display error message
MsgBox "Error: " & .Status & " - " & .StatusText
End If
End With
' Clean up
Set tokenRequest = Nothing
End Function
Function EncodeBase64(ByVal str As String) As String
Dim arrData() As Byte
Dim objXML As Object
' Convert string to byte array
arrData = StrConv(str, vbFromUnicode)
' Encode byte array to Base64
Set objXML = CreateObject("MSXML2.DOMDocument")
With objXML.CreateElement("b64")
.DataType = "bin.base64"
.NodeTypedValue = arrData
EncodeBase64 = .text
End With
Set objXML = Nothing
End Function
</code>
Public Function getTokentemp() As String
Dim url As String
Dim Auth As String
Dim headers As Object
Dim params As Object
Dim tokenRequest As Object
clientId = "CLIENTID GOES HERE"
clientSecret = "SECRET INFO HERE"
url = "https://URLHERE.com/oauth/token"
Auth = clientId & ":" & clientSecret
'MsgBox (Auth)
'MsgBox (url)
' Encode Auth string to Base64
Auth = EncodeBase64(Auth)
' Create headers dictionary
Set headers = CreateObject("Scripting.Dictionary")
headers("Authorization") = "Basic " & Auth
headers("Content-Type") = "application/json"
' Create parameters dictionary
Set params = CreateObject("Scripting.Dictionary")
params("grant_type") = "client_credentials"
params("x-sap-sac-custom-auth") = "true"
' Create HTTP request object
Set tokenRequest = CreateObject("MSXML2.ServerXMLHTTP")
With tokenRequest
.Open "POST", url, False
' Set request headers
For Each Key In headers
.setRequestHeader Key, headers(Key)
Next Key
' Send request with parameters
.send Join(params.Items, "&")
' Check response status code
If .Status = 200 Then
getTokentemp = .responseText
MsgBox getTokentemp
Else
' Display error message
MsgBox "Error: " & .Status & " - " & .StatusText
End If
End With
' Clean up
Set tokenRequest = Nothing
End Function
Function EncodeBase64(ByVal str As String) As String
Dim arrData() As Byte
Dim objXML As Object
' Convert string to byte array
arrData = StrConv(str, vbFromUnicode)
' Encode byte array to Base64
Set objXML = CreateObject("MSXML2.DOMDocument")
With objXML.CreateElement("b64")
.DataType = "bin.base64"
.NodeTypedValue = arrData
EncodeBase64 = .text
End With
Set objXML = Nothing
End Function