[英]How to add a OneNote notebook tab to Teams channel using Graph API and PowerShell?
I'm trying to add a tab containing a OneNote notebook into my MS Teams channel using Graph API.我正在尝试使用 Graph API 将包含 OneNote 笔记本的选项卡添加到我的 MS Teams 频道中。 I found this usefull blog post that is explaining how to do this.
我发现这篇有用的博客文章解释了如何做到这一点。 Unofortunetely I did not manage to go to the end.
不幸的是我没能走到最后。 I'm stuck to the step when he is creating a OneNote in the Teams, whatever that means.
当他在 Teams 中创建 OneNote 时,我一直坚持这一步,无论这意味着什么。
Here is a picture of my Microsoft Graph Explorer windows:这是我的 Microsoft Graph Explorer 窗口的图片:
FYI my Request body section contains the following code :仅供参考,我的请求正文部分包含以下代码:
{
"displayName": "OneNote"
}
Here is my PowerShell.这是我的 PowerShell。 The variables that are not initialized have actual values (personnal or irrelevant for this post).
未初始化的变量具有实际值(个人的或与本文无关的)。 They are properly defined because i'm getting all I want to.
它们被正确定义,因为我得到了我想要的一切。 Only the last request invocation is not working.
只有最后一次请求调用不起作用。
$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
When I launch the snippet above I'm getting the following message:当我启动上面的代码片段时,我收到以下消息:
Invoke-RestMethod : You must write ContentLength bytes to the request stream before calling [Begin]GetResponse
What am I doing wrong ?我究竟做错了什么 ?
Invoke-RestMethod 适用于“GET”操作,但它似乎不适用于 POST 操作,因此尝试将上面发布的最后一行,即带有“body”的 Invoke-WebRequest 转换为
$createdGroup = Invoke-WebRequest -Uri -Headers $headers -ContentType 'application/json' -Method "POST" -Body $body
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.