Using VBA to get data from API with bearer token to Excel

lala_Hae_dong

New Member
Joined
Apr 13, 2023
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Can this be done? How do I create the Authorization Header?
I have created the API Request , see below, but when I run I get the following message ,"{"status":"Internal Server Error","msg":[{"type":"ERROR","details":"The authorization header is either empty or isn't Basic."}]}" and I am unsure how to resolve.

Here is my current API Request

Sub SendAPIrequest()

Dim HTTPreq As Object, URL As String, response As String

Set HTTPreq = CreateObject("MSXML2.XMLHTTP")


URL = "myurl"

With HTTPreq
.Open "Get", URL, False
.send
End With

response = HTTPreq.responseText
Debug.Print response
Call ReadJSONresponse(response)

End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Maybe something like that (you have to change according to the provider authorization)
VBA Code:
Sub SendAPIrequest()
    Dim HTTPreq As Object, URL As String, response As String
    Dim authHeader As String, authCreds As String

    Set HTTPreq = CreateObject("MSXML2.XMLHTTP")

    URL = "myurl"
    authCreds = "username:password"
    authHeader = "Basic " & Base64Encode(authCreds)

    With HTTPreq
        .Open "Get", URL, False
        .setRequestHeader "Authorization", authHeader
        .send
    End With

    response = HTTPreq.responseText
    Debug.Print response
    Call ReadJSONresponse(response)
End Sub

Private Function Base64Encode(ByVal data As String) As String
    Dim enc As Object, oText As Object
    Set enc = CreateObject("System.Text.Encoding")
    Set oText = enc.EncodeBase64(enc.Getbytes_4(data))
    Base64Encode = oText
End Function
 
Upvote 0
I must be missing something?
I got the following error

Run-time error '429':
ActiveX component can't create object
 
Upvote 0
You need the .NET Framework library, which includes the System.Text.Encoding object. You can download the .NET Framework from the Microsoft website.
 
Upvote 0
I must be doing something wrong. I downloaded .net Framework but i cant find it in my reference tools
 
Upvote 0
Here's an alternative function which encodes a text string as Base64, using MSXML2 instead of System.Text.Encoding. No references are required.

VBA Code:
Private Function EncodeBase64(plainText As String) As String

    Dim bytes() As Byte
    Dim objXML As Object 'MSXML2.DOMDocument60
    Dim objNode As Object 'MSXML2.IXMLDOMNode
    
    bytes = StrConv(plainText, vbFromUnicode)
   
    Set objXML = CreateObject("MSXML2.DOMDocument.6.0")
    Set objNode = objXML.createElement("b64")
    objNode.DataType = "bin.base64"
    objNode.nodeTypedValue = bytes
    EncodeBase64 = objNode.Text
    
    Set objNode = Nothing
    Set objXML = Nothing
    
End Function
 
Upvote 0
Here's an alternative function which encodes a text string as Base64, using MSXML2 instead of System.Text.Encoding. No references are required.

VBA Code:
Private Function EncodeBase64(plainText As String) As String

    Dim bytes() As Byte
    Dim objXML As Object 'MSXML2.DOMDocument60
    Dim objNode As Object 'MSXML2.IXMLDOMNode
   
    bytes = StrConv(plainText, vbFromUnicode)
  
    Set objXML = CreateObject("MSXML2.DOMDocument.6.0")
    Set objNode = objXML.createElement("b64")
    objNode.DataType = "bin.base64"
    objNode.nodeTypedValue = bytes
    EncodeBase64 = objNode.Text
   
    Set objNode = Nothing
    Set objXML = Nothing
   
End Function
Thank you that worked perfectly
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,116
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top