【问题标题】:Error 400 bad request when creating a group calendar event in powershell with Microsoft Graph API使用 Microsoft Graph API 在 PowerShell 中创建组日历事件时出现错误 400 错误请求
【发布时间】:2020-09-04 00:05:37
【问题描述】:

我正在尝试通过 powershell 在 Office 365 组的日历中创建一个事件。

这是我第一次做这种编程的经历,如果我的问题很基本,很抱歉:-)

首先,我创建了一个简单的 json 文件(calendar.json)

{
  "start":
{
 "dateTime":"2017-03-12T17:00:00.0000000",
 "timeZone":"UTC"
},
"end":
{
 "dateTime":"2017-03-12T17:30:00.0000000",
 "timeZone":"UTC"
},
  "responseStatus": {
    "response": "None"
  },
  "iCalUId": "null",
  "isReminderOn": false,
  "subject": "Test Event created from API"
}

然后我通过以下步骤创建事件:

使用经过测试的 powershell 函数给我令牌 使用此代码添加标题:

$headers = @{}
$headers.Add('Authorization','Bearer ' + $token.AccessToken)
$headers.Add('Content-Type',"application/json")

因为我现在开始,我将json文件转换为一个对象,然后将对象转换为json(我知道,这很愚蠢,但我这样做是因为我不知道json以及如何无错误地转换在 powershell 代码中)

$json = ConvertFrom-Json -InputObject (Gc 'C:\Users\mmangiante\OneDrive - Interactive Media S.p.A\Office 365\calendar.json'-Raw)
$body = ConvertTo-Json $json

调用 Invoke-RestMethod

response = Invoke-RestMethod 'https://graph.microsoft.com/v1.0/groups/768afb0c-bafd-4272-b855-6b317a3a9953/calendar/events' -Method Post -Headers $headers -Body $json

返回的是 400 错误请求。

Sending Microsoft Graph request events returns 400一样的错误

鉴于对该问题的回答,我修改了代码以返回错误:

try{$restp=Invoke-RestMethod 'https://graph.microsoft.com/v1.0/groups/768afb0c-bafd-4272-b855-6b317a3a9953/calendar/events' -Method Post -Headers $headers -Body $json
} catch {$err=$_}
$err

就像How do I get the body of a web request that returned 400 Bad Request from Invoke-RestMethod 中的建议一样,但我没有发现任何感兴趣的东西。 我发现的唯一一件事是,在撰写本文时,Invoke-RestMethod 没有像 https://github.com/PowerShell/PowerShell/issues/2193 中那样返回完整的响应

我想我的 json 不是“格式正确”,但我不知道为什么。

有人有什么建议吗?

【问题讨论】:

    标签: powershell office365 microsoft-graph-api


    【解决方案1】:

    这种格式过去对我有用。如果这能解决您的问题,请告诉我:

    $headers = @{ 
        "Authorization" = ("Bearer {0}" -f $token);
        "Content-Type" = "application/json";
    }
    
    $body = @{
        Name1 = 'Value1';
        Name2 = 'Value2';
    }
    
    $bodyJSON = $body | ConvertTo-Json
    
    Invoke-RestMethod -Method Post -Uri <API HERE> -Headers $headers -Body $bodyJSON -OutFile $output
    

    【讨论】:

      【解决方案2】:

      谢谢肖恩,

      我尝试了您的建议,但没有运气;我用其他 2 个 api 做了其他测试,发现结果不一致。 第一个测试是使用联系人相关的graph api,创建联系人如https://graph.microsoft.io/en-us/docs/api-reference/v1.0/api/user_post_contacts

      我创建了示例 json 的 powershell 表示:

      $contactbody = @{
          givenName = 'Ciccio';
          surname = 'Patacca';
          emailAddresses = @(
              @{
                  address = 'cicciopatacca@cicciociccio.com'
                  name = 'Ciccio Patacca'
              }
          )
          businessPhones = (
              '+39 555 123 4567'
          )
      }
      

      然后转成json

      $contactbody = $contactbody | ConvertTo-Json
      

      我从 Azure 门户中检索到与我公司用户相关的对象 ID,因此我调用了 rest 方法:

      $response = Invoke-RestMethod 'https://graph.microsoft.com/v1.0/users/e37d2dbe-bdaf-4098-9bb0-03be8c653f7d/contact' -Method Post -Headers $headers -Body $contactbody
      

      最终结果是 400 bad request 错误。 因此,我尝试了另一个示例,并重用了从谷歌搜索中检索到的一些代码。 这次我复制了示例中的 powershell json 表示并进行了转换:

      $body = @{"displayName"="ps-blog"; "mailEnabled"=$false; "groupTypes"=@("Unified"); "securityEnabled"=$false; "mailNickname"="ps1" } | ConvertTo-Json
      

      并且,正如https://graph.microsoft.io/en-us/docs/api-reference/v1.0/api/group_post_groups 中所述,我调用了 rest 方法

      $response = Invoke-RestMethod 'https://graph.microsoft.com/v1.0/groups' -Method Post -Headers $headers -Body $body
      

      这很有效。 所以我认为以前错误样本的powershell表示没有正确形成(即使我打印它时它们等于图形api上的样本);为了测试,我将正文的最后一个 powershell 重写为:

      $body = @{
          displayName = 'TestGraphGroup';
          mailEnabled = $true;
          groupTypes = @('Unified')
          securityEnabled = $false;
          mailNickname = 'TestGraphGroup'
      }
      
      $body = $body | ConvertTo-Json
      

      并调用了最后一个方法:它再次起作用了。

      那么,我哪里做错了?

      【讨论】:

        【解决方案3】:

        发布了一个新的 SDK,使这更容易。 结帐说明如何使用它here

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-09-06
          • 2021-09-07
          • 1970-01-01
          • 2020-04-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多