如何使用Graph API和PowerShell将OneNote笔记本选项卡添加到团队频道?

如何使用Graph API和PowerShell将OneNote笔记本选项卡添加到团队频道?,powershell,microsoft-graph-api,microsoft-teams,onenote,Powershell,Microsoft Graph Api,Microsoft Teams,Onenote,我正在尝试使用Graph API将包含OneNote笔记本的选项卡添加到我的MS团队频道。 我发现这是解释如何做到这一点。不幸的是,我没能走到最后。 当他在团队中创建OneNote时,无论这意味着什么,我都坚持这一步 以下是我的Microsoft Graph Explorer窗口的图片: 仅供参考,我的请求正文部分包含以下代码: { "displayName": "OneNote" } 这是我的动力地狱。未初始化的变量具有实际值(personn

我正在尝试使用Graph API将包含OneNote笔记本的选项卡添加到我的MS团队频道。 我发现这是解释如何做到这一点。不幸的是,我没能走到最后。 当他在团队中创建OneNote时,无论这意味着什么,我都坚持这一步

以下是我的Microsoft Graph Explorer窗口的图片:

仅供参考,我的请求正文部分包含以下代码:

{
    "displayName": "OneNote"
}
这是我的动力地狱。未初始化的变量具有实际值(personnal或与本文无关)。它们是正确定义的,因为我得到了我想要的一切。只有最后一次请求调用不起作用

$graphAPIUrl = "https://graph.microsoft.com/v1.0/"
$teamTitle = "My teams"

# Connecting to AzureAD Services
Connect-AzureAD -Credential $creds

# Connecting to Graph API services
Connect-PnPOnline -ClientId $GraphAppId -ClientSecret $GraphAppSecret -AADDomain $AADDomain

# Getting access token
$token = Get-PnPGraphAccessToken
$headers = @{
    "Authorization"="Bearer " + $token;
    "Content-Type"= "application/json";
    "Content-length" = 200;
}

# Getting current team group ID
$getAllGroupsRequest = "$($graphAPIUrl)groups"
$groups = Invoke-RestMethod -Uri $getAllGroupsRequest -Headers $headers -Method "GET"
$groupId = ($groups.value | Where-Object displayName -eq $teamTitle).id

# Getting site collection ID
$getRootSiteInfoRequest = "$($graphAPIUrl)groups/$($groupId)/sites/root"
$siteCollectionInfos = Invoke-RestMethod -Uri $getRootSiteInfoRequest -Headers $headers -Method "GET"

# Getting site ID
$siteCollectionId = (($siteCollectionInfos.value).id -Split ",")[1]
$siteId = (($siteCollectionInfos.value).id -Split ",")[2]

# Creating the notebook in teams
$createNoteBookRequest = "$($graphAPIUrl)groups/$($groupId)/onenote/notebooks"
$body = @{
    "displayName" = "OneNote"
}
$formatedBody = ConvertTo-Json -InputObject $body
$createdGroup = Invoke-RestMethod -Uri $getRootSiteInfoRequest -Headers $headers -Method "POST" -Body $body
当我启动上面的代码段时,我得到以下消息:

Invoke-RestMethod : You must write ContentLength bytes to the request stream before calling [Begin]GetResponse

我做错了什么?

Invoke rest方法对于“GET”操作很好,但是对于POST操作似乎不太好,所以尝试将上面发布的最后一行,带有“body”的Invoke WebRequest转换为


我不明白你的答案。我的请求中已经有了一个主体。啊,我的错误——没有正确阅读我自己的答案!“Invoke RestMethod”将更改为“Invoke WebRequest”-请参阅更新的回答Yes。在应用您的提示后,我仍然会收到相同的错误消息。这需要更多的工作,但您可以尝试直接使用webclient,它支持这两个命令-有关更多信息,请参阅此处:
$createdGroup = Invoke-WebRequest -Uri -Headers $headers -ContentType 'application/json' -Method "POST" -Body $body