简体   繁体   English

将请求发布到 Microsoft graph API

[英]Post request to Microsoft graph API

I'm trying to request an access token with vba code.我正在尝试使用 vba 代码请求访问令牌。 When putting all the parameters I give in VBA into postman I get an access token no problem but when using it in VBA code it gives the error将我在 VBA 中提供的所有参数放入 postman 时,我得到一个访问令牌没问题,但是在 VBA 代码中使用它时会出现错误

"the request body must contain the following parameter: 'grant_type'" “请求正文必须包含以下参数:'grant_type'”

    Dim objRequestGetToken As Object, JSONGetToken As Object
    Dim strUrlGetToken As String, strClientID As String, strClientSecret As String, strResource As String, strBody As String
    Dim blnAsyncGetToken As Boolean

    strClientID = "XXX"
    strClientSecret = "XXX"
    strRecourse = "00000003-0000-0ff1-ce00-000000000000/XXX.sharepoint.com@XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
    
    strBody = "{""grant_type"":""client_credentials"",""client_id"":""" & strClientID & """,""client_secret"":""" & strClientSecret & """,""resource"":""" & strRecourse & """}"

    Set objRequestGetToken = CreateObject("MSXML2.XMLHTTP.6.0")
    strUrlGetToken = "https://accounts.accesscontrol.windows.net/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/tokens/OAuth/2/"
    blnAsyncGetToken = False
    Debug.Print strBody

    With objRequestGetToken
        .Open "POST", strUrlGetToken, blnAsyncGetToken
        .SetRequestHeader "Content-Type", "application/json;odata=verbose"
        .SetRequestHeader "Accept", "application/json;odata=verbose"
        .send strBody
        'spin wheels whilst waiting for response
        While objRequestGetToken.readyState <> 4
            DoEvents
        Wend
        Set JSONGetData = JsonConverter.ParseJson(objRequestGetToken.ResponseText)
    End With

    Debug.Print objRequestGetToken.ResponseText

I know this is a possible duplicate request with VBA post method request body ("MSXML2.XMLHTTP"): Error Parsing JSON: ^ Expecting '{' or '[' but I can't figure out why my body is not properly being sent and the post previously mentioned doesn't seem to help.我知道这可能是VBA 后方法请求正文(“MSXML2.XMLHTTP”)的重复请求:解析 JSON 时出错:^ 期待 '{' 或 '['但我不知道为什么我的正文没有正确发送前面提到的帖子似乎没有帮助。

postman request: postman 请求: 在此处输入图像描述在此处输入图像描述

Any kind of help would be much appreciated!任何形式的帮助将不胜感激!

Edit: Here's the code that made it work eventually:)编辑:这是使它最终工作的代码:)

    Dim objRequestGetToken As Object, JSONGetToken As Object
    Dim strUrlGetToken As String, strClientID As String, strClientSecret As String, strResource As String, strBody As String
    Dim blnAsyncGetToken As Boolean

    strClientID = "XXX"
    strClientSecret = "XXX"
    strRecourse = "00000003-0000-0ff1-ce00-000000000000/XXX.sharepoint.com@XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
    
    strBody = "{""grant_type"":""client_credentials"",""client_id"":""" & strClientID & """,""client_secret"":""" & strClientSecret & """,""resource"":""" & strRecourse & """}"

    Set objRequestGetToken = CreateObject("MSXML2.XMLHTTP.6.0")
    strUrlGetToken = "https://accounts.accesscontrol.windows.net/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/tokens/OAuth/2/"
    blnAsyncGetToken = False
    Debug.Print strBody

    With objRequestGetToken
        .Open "POST", strUrlGetToken, blnAsyncGetToken
        .SetRequestHeader "application", "x-www-form-urlencoded"
        .SetRequestHeader "Accept", "application/json;odata=verbose"
        .send strBody
        'spin wheels whilst waiting for response
        While objRequestGetToken.readyState <> 4
            DoEvents
        Wend
        Set JSONGetData = JsonConverter.ParseJson(objRequestGetToken.ResponseText)
    End With

    Debug.Print objRequestGetToken.ResponseText
strBody = "grant_type=client_credentials&client_id=" & strClientID & "&client_secret=" & strClientSecret & "&resource=" & strRecourse

and changing the header to x-www-form-urlencoded并将 header 更改为x-www-form-urlencoded

With objRequestGetToken
    .Open "POST", strUrlGetToken, blnAsyncGetToken
    .SetRequestHeader "application", "x-www-form-urlencoded"
    .SetRequestHeader "Accept", "application/json;odata=verbose"
    .send strBody
    'spin wheels whilst waiting for response
    While objRequestGetToken.readyState <> 4
        DoEvents
    Wend
    Set JSONGetData = JsonConverter.ParseJson(objRequestGetToken.ResponseText)
End With

should solve the issue.应该解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM