OneDrive FB使用Microsoft Graph API Powershell创建文件夹



我只想通过Graph API创建一个文件夹,一个我的OneDrive for Buiness帐户。几个小时后,我陷入了一个我真的无法理解的错误。它说属性"Attributes"在类型"oneDrive.folder"上不存在,我不应该使用此属性。问题是我没有使用这个属性。经过大量的研究,我认为这与固定元数据或类似的东西有关。但总的来说,我真的不知道以后该怎么办。

我使用图形资源管理器和另一个网站创建了这个脚本。

错误:

-1, Microsoft.SharePoint.Client.InvalidClientQueryException 
The property 'Attributes' does not exist on type 'oneDrive.folder'.
Make sure to only use property names that are defined by the type.

这是我的代码:

$clientId = "XXXXXXXXXXXXXX"
$tenantId = "XXXXXX.onmicrosoft.com"
$clientSecret = 'XXXXXXXXXXXX'

$uri = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"

$body = @{
client_id     = $clientId
scope         = "https://graph.microsoft.com/.default"
client_secret = $clientSecret
grant_type    = "client_credentials"
}
$tokenRequest = Invoke-WebRequest -Method Post -Uri $uri -ContentType "application/x-www-form-urlencoded" -Body $body -UseBasicParsing
$token = ($tokenRequest.Content | ConvertFrom-Json).access_token
Write-Host $token

$uri = "https://graph.microsoft.com/v1.0/<ID XXXXXXX>/drive/root/children"
$method = 'POST'
$head= @{Authorization = "Bearer $token"} 
$postPara= @{
name= "NewFolder"
folder= {}
} | ConvertTo-Json

$antwort = Invoke-RestMethod -Headers $head -Uri $uri -Method $method -Body $postPara -ContentType "application/json"
Write-Host $antwort

它真的应该起作用,我已经在这个示例任务上坐了10多个小时了。

代码的问题是

$postPara= @{
name= "NewFolder"
folder = {}
} | ConvertTo-Json

如果你只输出$postPara,你会发现问题是因为你在文件夹中的值前面缺少@,你实际上会从那里填充的底层脚本中获得详细信息。所以试试

$postPara= @{
name= "NewFolder"
folder = @{}
} | ConvertTo-Json
$postPara

一个好的诊断工具也是使用fiddler来查看发送到服务器的请求。

最新更新