How to call Microsoft Graph API using VBA?

前端 未结 1 1174
悲&欢浪女
悲&欢浪女 2020-12-07 03:34

Question

Is it possible to call Microsoft Graph API using VBA code?

If yes, how to handle O365 authorization? I have seen plenty of topics

相关标签:
1条回答
  • 2020-12-07 04:08

    So the code you show is only partially correct. Here is what I found to actually work. (This is with what you provided as I actually found a Json parser to work with the data better than the innerHTML methods, I also had to use a different version of MSXML since the one you reference wasnt working for me.)

    Function GetToken()
        Dim xml As New MSXML2.XMLHTTP60
        Dim doc As MSHTML.HTMLDocument
        Dim urltoken As String
    
        'copy paste the URL you see when calling Microsoft Graph Explorer and add prompt + domain_hint parameters
        urltoken = "https://login.microsoftonline.com/{tenent id}/oauth2/v2.0/token"
    
        xml.Open "POST", urltoken, False
    
        xml.Send("client_id={clientid}&scope=https://graph.microsoft.com/.default&grant_type=client_credentials&client_secret=(cleint secret}")
    
        If xml.readyState = 4 And xml.Status = 200 Then
            Set doc = New MSHTML.HTMLDocument
            doc.Body.innerHTML = xml.responseText
    
            GetToken = doc.getElementsByName("access_token")(0).Value
    
            sSuccess = True
        Else
             MsgBox "Error" & vbNewLine & "Ready state: " & xml.readyState & _
             vbNewLine & "HTTP request status: " & xml.Status
             sSuccess = False
        End If
    
        Set xml = Nothing
    End Function
    
    0 讨论(0)
提交回复
热议问题