vba - 如何使用 VBA 调用 Microsoft Graph API?
问题描述
问题
是否可以使用 VBA 代码调用 Microsoft Graph API?
如果是,如何处理O365授权?我已经看到很多话题都说要在 Microsoft Azure 中创建一个应用程序来获取令牌,但我很惊讶我必须为简单的本地使用而这样做。
我试过的
发现 Microsoft Graph 后,我在 Graph Explorer https://graph.microsoft.com/v1.0/planner/tasks中尝试了这个 API
我能够在计划器中创建任务!
因此,在我看来,可以从直接在 Outlook 中执行的 VBA 代码调用此 API。
我在 Outlook 中创建了这个宏:
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)
我有一个授权错误
错误代码 401
2020 年 3 月 12 日更新: 找到在调用 Graph Explorer 时获取 Graph Api 令牌分析 URL 的解决方案(非常适合我):
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&nonce=graph_explorer&mkt=fr-FR&client_id={clientid}&response_type=token&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&prompt=none&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" & vbNewLine & "Ready state: " & xml.readyState & _
vbNewLine & "HTTP request status: " & xml.Status
sSuccess = False
End If
Set xml = Nothing
End Function
所以使用 VBA 调用 Graph API 是可能的 :)
解决方案
因此,您显示的代码仅部分正确。这是我发现实际有效的方法。(这与您提供的内容有关,因为我实际上发现一个 Json 解析器比 innerHTML 方法更好地处理数据,我还必须使用不同版本的 MSXML,因为您引用的那个对我不起作用。)
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
推荐阅读
- c++ - 当我有两个指针在同一个内存时,为什么我必须调用两次“删除”?
- java - 在房间版本 1.1.1 中创建复合主键时无法使用 Kotlin 编译器构建 android studio 项目
- javascript - 在没有只读错误的情况下无法访问深层嵌套对象属性
- python - 如何在 python 中使用 requests.get 获取数据之前等待页面加载,而不使用 api
- java - Repaint() 方法不会一一调用paint() 和paintComponent() 方法,只有paintComponent() 方法有效
- unity3d - 你可以在 Unity 中导出切片精灵(PNG 图像)吗?
- php - php 日期比较
- pseudocode - 如何编写伪代码
- codeigniter - localhost 当前无法处理此请求。代码点火器问题
- jsf - 通过 ajax 调用使用模式“MMMM yyyy”更新组件 primefaces datepicker 会导致“位置 [...] 处未捕获的名称”异常