Microsoft Teams Graph API:向团队添加通道失败,返回404



我正在尝试使用Microsoft Graph API并使用Invoke-RestMethod将通道添加到新创建的Microsoft Teams中,在Azure Functions 中运行以下Powershell

我尝试了v1.0和beta版本,并在高和低版本中搜索了以下示例:

# code that successfully creates a team based on existing Office 365 Group not shown, but is
# comes from [https://github.com/martinagrom/Ignite2018GroupsGovernanceToolkit][1], f1-CreateGroup and f2-CreateTeam
function AddContent ($folderName) {
Write-Output "Inside of AddContent"
$Body = @{
"displayName"= "$folderName"
"description"= "Add decription"
}
$bodyJSON = $body | ConvertTo-Json
Write-Output "Adding channel $($folderName)"
Write-Output "bodyJSON $($bodyJSON)"
try {
Write-Output "creating channel on https://graph.microsoft.com/beta/teams/$($TeamID)/channels"
$result = Invoke-RestMethod `
-Method POST `
-Uri "https://graph.microsoft.com/beta/teams/$($TeamID)/channels" `
-ContentType 'application/json' `
-Headers $script:APIHeader `
-Body $bodyJSON `
-ErrorAction Stop
$TeamResult = $result.id
Write-Output "Channel created: ID:[$TeamResult]"
} catch [System.Net.WebException] {
Write-Output "AddChannelOnExistingTeam: $($TeamResult.error.code) => $($TeamResult.error.message)"
Write-Output "https://graph.microsoft.com/beta/teams/$($TeamID)/channels   ---    $script:APIHeader --- $body"
If ($_.Exception.Response.StatusCode.value__) {
$crap = ($_.Exception.Response.StatusCode.value__ ).ToString().Trim();
Write-Output $crap;
}
If  ($_.Exception.Message) {
$crapMessage = ($_.Exception.Message).ToString().Trim();
Write-Output $crapMessage;
}
$result = $_.Exception.Response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($result)
$reader.BaseStream.Position = 0
$reader.DiscardBufferedData()
$responseBody = $reader.ReadToEnd();
Write-Output $responseBody
throw "AddChannelOnExistingTeam: $($TeamResult.error.code) => $($TeamResult.error.message)"
}
catch {
Write-Output "AddChannelOnExistingTeam: Another Exception caught: [$($_.Exception)]"
throw "AddChannelOnExistingTeam: $($TeamResult.error.code) => $($TeamResult.error.message)"
}
}
# The team has just been created based on an Office365 group by code (not shown) and the Group/Team ID is in the $TeamResult
# I do a sleep to ensure Team is ready, probably not necessary. Plenty of Write-Out to debug progress. Log futher down
# I grap a list of folder names from a document library and is calling the above function with each name to create a corrosponding channel
Start-Sleep -Seconds 60
$TeamID = $TeamResult
$TeamChannels = $TeamInfo.TeamChannels
Write-Output "TeamChannels $($TeamChannels) on TeamID $($TeamID)"
$sourcesite = $TeamChannels.SubString(0,$TeamChannels.lastIndexOf('/'))
$secpasswd = ConvertTo-SecureString $env:SPO_P -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ($env:SPO_U, $secpasswd)
connect-PnPOnline -Credentials $mycreds -url $sourcesite 
$folders = Get-pnpFolderItem -FolderSiteRelativeUrl $TeamChannels.SubString($TeamChannels.lastIndexOf('/')+1)
ForEach ($folder in $folders) {
if ($folder.Name -ne "Forms") {
AddContent($folder.Name)
}
}

从Martina Grom 创建团队代码

日志输出:

2018-12-10T12:15:07.373 [Info] TeamChannels https://contoso.sharepoint.com/sites/Projects/Template on TeamID xxxxxx-e3ab-4e3a-b81d-c540c8e2e6ea
2018-12-10T12:15:08.889 [Info] Inside of AddContent
2018-12-10T12:15:08.889 [Info] Adding channel 01. Gate - Documentation
2018-12-10T12:15:08.889 [Info] bodyJSON {
"displayName":  "01. Gate - Documentation",
"description":  "Add decription"
}
2018-12-10T12:15:08.889 [Info] creating channel on https://graph.microsoft.com/beta/teams/xxxxxx-e3ab-4e3a-b81d-c540c8e2e6ea/channels
2018-12-10T12:15:08.920 [Info] AddChannelOnExistingTeam:  =>
2018-12-10T12:15:08.920 [Info] https://graph.microsoft.com/beta/teams/xxxxxx-e3ab-4e3a-b81d-c540c8e2e6ea/channels   ---    System.Collections.Hashtable --- System.Collections.Hashtable
2018-12-10T12:15:08.920 [Info] 404
2018-12-10T12:15:08.920 [Info] The remote server returned an error: (404) Not Found.
2018-12-10T12:15:08.920 [Info] {
"error": {
"code": "UnknownError",
"message": "{"message":"No HTTP resource was found that matches the request URI 'https://api.teams.skype.com/beta/teams('xxxxxx-e3ab-4e3a-b81d-c540c8e2e6ea')/channels'."}",
"innerError": {
"request-id": "10523b5c-55b7-4a2e-a428-f94b7f0fa88a",
"date": "2018-12-10T12:15:08"
}
}
}
2018-12-10T12:15:08.952 [Error] Exception while executing function: Functions.CreateTeam. System.Management.Automation: AddChannelOnExistingTeam:  => . AddChannelOnExistingTeam:  => .
2018-12-10T12:15:08.983 [Error] Function completed (Failure, Id=xxxxxxx-52cd-45c7-9bd8-5e509353ddfc, Duration=64753ms)
# End of Log

因此POSTto https://graph.microsoft.com/beta/teams/TeamID/channels恢复为https://api.teams.skype.com/beta/teams('TeamID')/channels,并以404失败

如果我转到Microsoft图形资源管理器并输入与创建通道完全相同的信息。

我错过了什么?是否缺少API权限?如有任何提示,不胜感激。

好的,我发现了错误,纯复制粘贴失明…:(

我复制的Invoke-RestMethod是一个PUT方法,当您尝试使用它来创建通道时,会出现404错误。

所以当我把它改成POST方法时,它就开始工作了。我编辑了来源。

最新更新