How to pass authentication credentials in VBA

前端 未结 3 823
庸人自扰
庸人自扰 2020-12-10 16:31

I\'m trying to write a VBA macro that would pass my credentails to an address and fetch some content (REST API for JIRA), but I\'m having some difficulties converting my cod

3条回答
  •  伪装坚强ぢ
    2020-12-10 17:16

    Use the "Authorization" request header. The “Authorization” request header requires an encrypted string for username and password.

    .setRequestHeader "Authorization", "Basic " & EncodeBase64
    

    So here JiraService being an XMLHTTP object, something like this will authenticate, where EncodeBase64 is a function which returns encrypted string.

    Public Function isAuthorized() As Boolean
    With JiraService
        .Open "POST", sURL & "/rest/api/2/issue/", False
        .setRequestHeader "Content-Type", "application/json"
        .setRequestHeader "Accept", "application/json"
        .setRequestHeader "Authorization", "Basic " & EncodeBase64
        .send ""
        If .Status <> 401 Then
            isAuthorized = True
        Else
            isAuthorized = False
        End If
    End With
    
    Set JiraService = Nothing
    End Function
    

    You can check out a complete VBA example here

提交回复
热议问题