0

Below code is where I get 401 Unautorized access error After 2nd request Rest API is called .

Here is the developer Rest API documentation supplied. https://simplybook.me/en/api/developer-api/tab/rest_api#method_GET_/admin/bookings/{id}

Sub Test()

    Dim objRequest As Object
    Dim loginResponse As Object
    Dim strUrl As String
    Dim bookingcode As String
    Dim blnAsync As Boolean
    Dim strloginResponse, strBookingresponse, strlogoutresponse, strparameter As String
    Dim scriptControl As Object

    Set scriptControl = CreateObject("MSScriptControl.ScriptControl")
    scriptControl.Language = "JScript"
    
    Set objRequest = CreateObject("MSXML2.XMLHTTP")
    'strUrl = "https://user-api.simplybook.me/admin/auth"
    strUrl = "https://user-api-v2.simplybook.me/admin/auth"
    blnAsync = True
    
    'strUrl = "https://user-api.simplybook.me/admin/auth"
    strUrl = "https://user-api-v2.simplybook.me/admin/auth"
     With objRequest
        .Open "POST", strUrl, blnAsync
        .SetRequestHeader "Content-Type", "application/json"
        .Send ("{""company"":""simplydemo"",""login"":""admin"",""password"":""demo""}")
        'spin wheels whilst waiting for response
        While objRequest.readyState <> 4
            DoEvents
        Wend
        strloginResponse = .responsetext
    End With

    Set loginResponse = scriptControl.Eval("(" + strloginResponse + ")")
    Debug.Print loginResponse.token
    Debug.Print loginResponse.company
    Debug.Print loginResponse.login
    Debug.Print loginResponse.refresh_token
    Debug.Print loginResponse.domain
    Debug.Print loginResponse.require2fa
    Debug.Print loginResponse.allowed2fa_providers
    Debug.Print loginResponse.auth_session_id
    
    bookingcode = "0bz29vl4t"
    strUrl = "https://user-api-v2.simplybook.me/admin/bookings/" & bookingcode
    'strUrl = "https://user-api.simplybook.me/admin/bookings/" & bookingcode
    strparameter = "(""{""X-Company-Login"":""" & loginResponse.company & """,""X-Token"":""" & loginResponse.token & """}"")"
    Debug.Print strparameter
    Set objRequest = CreateObject("MSXML2.XMLHTTP")
    With objRequest
        .Open "GET", strUrl, blnAsync
        .SetRequestHeader "Content-Type", "application/json"
        .Send strparameter
        'spin wheels whilst waiting for response
        While objRequest.readyState <> 4
            DoEvents
        Wend
        strBookingresponse = .responsetext
    End With
      Debug.Print strBookingresponse
    
    Set objRequest = CreateObject("MSXML2.XMLHTTP")
    strUrl = "https://user-api-v2.simplybook.me/admin/auth/logout"
    strparameter = "(""{""auth_token"":""" & loginResponse.token & """}"")"
    With objRequest
        .Open "POST", strUrl, blnAsync
        .SetRequestHeader "Content-Type", "application/json"
        .Send strparameter
        'spin wheels whilst waiting for response
        While objRequest.readyState <> 4
            DoEvents
        Wend
        strlogoutresponse = .responsetext
    End With
  Debug.Print strBookingresponse
  Set scriptControl = Nothing
  Set objRequest = Nothing
  Set loginResponse = Nothing
  
End Sub

Below response is i get for the Debug.print statements:

4eb8b7f6ce820c24e66a754557635ede02e6e808ce27e311511a20bef4ed44e3
simplydemo
admin
b60ee1e89264ee5e856583fc20e63926e71a25bded9f9a540d12d5ccbf51bf51
simplybook.me
False


("{"X-Company-Login":"simplydemo","X-Token":"4eb8b7f6ce820c24e66a754557635ede02e6e808ce27e311511a20bef4ed44e3"}")
{"code":401,"message":"Unauthorized","data":[],"message_data":[]}
{"code":401,"message":"Unauthorized","data":[],"message_data":[]}

Pᴇʜ
  • 45,553
  • 9
  • 41
  • 62
  • Your first fetch uses POST, with a request body, but the second is using get, also with a request body (which has parentheses?) From the API docs it looks like you should be passing X-Company-Login and x-Token as *headers* in that second call, not as JSON in the request body (which I don't think is used for GET requests) – Tim Williams Dec 14 '20 at 04:41
  • Using a request body with GET: https://stackoverflow.com/questions/978061/http-get-with-request-body – Tim Williams Dec 14 '20 at 05:21
  • Thank You for your suggestion. After changing to request header now i get . {"code":404,"message":"Not Found","data":[],"message_data":[]} Set objRequest = CreateObject("MSXML2.XMLHTTP") With objRequest .Open "GET", strUrl, blnAsync .SetRequestHeader "Content-Type", "application/json" .SetRequestHeader "X-Company-Login", loginResponse.company .SetRequestHeader "X-Token", loginResponse.token .Send strBookingresponse = .responseText End With Debug.Print strBookingresponse – user1710923 Dec 14 '20 at 07:21
  • What i am missing still now i get 404 Not Found – user1710923 Dec 14 '20 at 07:34
  • If you have updates to add, please edit your question and add thm there. Code is difficult to read in comments. – Tim Williams Dec 14 '20 at 16:18

0 Answers0