authorization value should atleast be `api_key`:`access_token` error in Excel VBA

ayush_001
I am testing 2 codes written in Excel VBA. The first function Get_Instruments returns all the instruments listed on an exchange. It works fine. However, the other function, OHLC_1_min data does not work. It returns an error "authorization value should atleast be `api_key`:`access_token`" (Image 3). Please tell me where am I going wrong. Please notice that I am referencing the correct cell where apikey and access token is stored. If that was the issue, Get_Instruments would not work at all.

Function Get_Instruments(exc As String) As Boolean

Dim acc_token As String, respo As String, apikey As String, HTTPReq As Variant, endpoint As String
apikey = ThisWorkbook.Worksheets("Sheet6").Cells(2, 7).Value
acc_token = ThisWorkbook.Worksheets("Sheet6").Cells(9, 7).Value
Set HTTPReq = CreateObject("WinHttp.WinHttpRequest.5.1")
endpoint = "https://api.kite.trade/instruments/" & exc

HTTPReq.Open "GET", endpoint, False
HTTPReq.Option(6) = False
HTTPReq.Option(12) = True
HTTPReq.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
HTTPReq.setRequestHeader "X-Kite-Version", "3"
HTTPReq.setRequestHeader "Authorization", "token " + EncodeBase64(apikey + ":" + acc_token)
HTTPReq.send
respo = HTTPReq.responseText
ThisWorkbook.Worksheets("Sheet7").Cells(10, 12).Value = respo

Dim exitcheck As Boolean
exitcheck = True
Get_Instruments = exitcheck

End Function

Function OHLC_1_min(ins_token As String, from_str As String, to_str As String) As Boolean

Dim acc_token As String, respo As String, apikey As String, HTTPReq As Variant, endpoint As String
apikey = ThisWorkbook.Worksheets("Sheet6").Cells(2, 7).Value
acc_token = ThisWorkbook.Worksheets("Sheet6").Cells(9, 7).Value
Set HTTPReq = CreateObject("WinHttp.WinHttpRequest.5.1")
endpoint = "https://api.kite.trade/instruments/historical/" & ins_token & "/" & "minute?" & "from=" & from_str & "&" & "to=" & to_str
'endpoint = "https://webhook.site/09ee8759-655c-4c76-a824-534f83844b2d"

HTTPReq.Open "GET", endpoint, False
'HTTPReq.Option(4) = 13056
HTTPReq.Option(6) = False
HTTPReq.Option(12) = True
HTTPReq.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
HTTPReq.setRequestHeader "X-Kite-Version", "3"
HTTPReq.setRequestHeader "Authorization", "token " + EncodeBase64(apikey + ":" + acc_token)
HTTPReq.send
respo = HTTPReq.responseText
ThisWorkbook.Worksheets("Sheet7").Cells(1, 1).Value = respo

' Dim JSON_Data As String, pewpew() As Variant
' JSON_Data = respo
' Set dic = ParseJSON(JSON_Data)
'
' pewpew = dic("obj.data.candles")
' Dim i As Variant
' For i = LBound(pewpew) To UBound(pewpew) Step 1
' ThisWorkbook.Worksheets("Sheet7").Cells(1 + i, 1).Value = pewpew(i)
' Next i

Dim exitcheck As Boolean
exitcheck = True
OHLC_1_min = exitcheck

End Function
  • sujith
    GetInstruments doesn't do authentication. You can ignore first API call working.
    Maybe don't do Base64 encoding while fetching historical data.

    PS: Do you have add-on historical data subscription?
  • ayush_001
    @sujith you are right. I have resolved the issue. Base64 encoding is not required. I simply had to do
    encode_str = apikey & ":" & acc_token
    and that was it.

    Yes, I have Historical data subscription.
This discussion has been closed.