title: How to call Microsoft Graph API using VBA?
body: <p><strong>Question</strong>  </p>
  
  <p>Is it possible to call Microsoft Graph API using VBA code?</p>
  
  <p>If yes, how to handle O365 authorization? I have seen plenty of topics saying to create an application in Microsoft Azure to get a token but I am surprised I must do that for a simple local use.</p>
  
  <p><strong>What I tried</strong></p>
  
  <p>After discovering Microsoft Graph, I tried this API in Graph Explorer
  <a href="https://graph.microsoft.com/v1.0/planner/tasks" rel="nofollow noreferrer">https://graph.microsoft.com/v1.0/planner/tasks</a></p>
  
  <p>I was able to create a task in planner!</p>
  
  <p>Consequently, in my mind, it was possible to call this API from VBA code executed directly in Outlook.</p>
  
  <p>I created this macro in Outlook:</p>
  
  <pre><code>Sub TaskPlannerCreation()
  
      Dim PlannerService As New MSXML2.XMLHTTP60
      Dim sData As Variant
  
      sData = " { ""  ""planId"": ""K9Zv2QHm1U-GSAhd-PTGZfdFeOn"",""bucketId"": ""b6NVNiEIQkGZeBBzn7kWqJvAGvvs"",""title"": ""Outlook task"" } "
  
      With PlannerService
          .Open "POST", "https://graph.microsoft.com/v1.0/planner/tasks", False
          .setRequestHeader "Content-Type", "application/json"
          .setRequestHeader "Accept", "application/json"
          .setRequestHeader "User-Agent", "xx"
          .Send (sData)
  </code></pre>
  
  <p>I have an Authorization error with </p>
  
  <blockquote>
    <p>error code 401</p>
  </blockquote>
  
  <p><strong>UPDATE on 12-03-2020 :</strong>
  Solution found to get a Graph Api token analysing URL when calling Graph Explorer (works perfectly for me) :</p>
  
  <pre><code>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/common/oauth2/v2.0/authorize?response_mode=form_post&amp;nonce=graph_explorer&amp;mkt=fr-FR&amp;client_id={clientid}&amp;response_type=token&amp;scope=openid profile User.ReadWrite User.ReadBasic.All Sites.ReadWrite.All Contacts.ReadWrite People.Read Notes.ReadWrite.All Tasks.ReadWrite Mail.ReadWrite Files.ReadWrite.All Calendars.ReadWrite&amp;prompt=none&amp;domain_hint=organizations"
  
      xml.Open "GET", urltoken, False
  
      xml.Send
  
      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" &amp; vbNewLine &amp; "Ready state: " &amp; xml.readyState &amp; _
           vbNewLine &amp; "HTTP request status: " &amp; xml.Status
           sSuccess = False
      End If
  
      Set xml = Nothing
  
  End Function
  </code></pre>
  
  <p>So using VBA for calling Graph API is possible :)</p>