Sunday, September 4, 2022

[SOLVED] Connect to API using vba

Issue

I need help to get authorization to connect to an API. The command line given is

curl -X GET "https://app.mysite.fr/api/v1/societe/11111111" -H "Authorization: Bearer 84dcc64aefabcdefghebcf7762752xx"

Below is my code written in VBA in ms-access

Function GetAuthorization()
    
    Dim objCurlHttp As Object
    Dim strResult As String
    Dim varReqToken As Variant
    Dim strWebServiceUrl As String
    Dim strOwnerKey As String
    Dim strQry As String
    
    strWebServiceUrl = "https://app.mysite.fr/api/v1/societe/11111111"
    strOwnerKey = "84dcc64aefabcdefghebcf776275xx"
    
    Set objCurlHttp = CreateObject("MSXML2.serverXMLHTTP")
                                                          
    
    With objCurlHttp
        .Open "GET", strWebServiceUrl, False
        
        .SetRequestHeader "cache-control", "no-cache"
        .SetRequestHeader "Content-type", "application/json"
        .SetRequestHeader "Content-type", "Accept"
        .SetRequestHeader "Authorization", "Bearer " + strOwnerKey
        .Send
        
        strResult = .ResponseText
        Debug.Print strResult
    End With
    
    Set objCurlHttp = Nothing
    
End Function

This result is

? GetAuthorization() Unauthorized

Thanks a lot in advance.


Solution

You don't reveal the documentation, but I seriously doubt that it states the Accept header as shown. So try:

        .Open "GET", strWebServiceUrl, False
        
        .SetRequestHeader "Content-type", "application/json"
        .SetRequestHeader "Accept", "application/json"
        .SetRequestHeader "Authorization", "Bearer " + strOwnerKey


Answered By - Gustav
Answer Checked By - Pedro (WPSolving Volunteer)