【发布时间】:2021-10-29 18:44:54
【问题描述】:
我正在尝试通过 Graph API 为特定的 SharePoint 网站设置权限。在邮递员中,我执行以下操作:
这行得通。但是,当我尝试在 PowerShell 中执行相同操作时,我收到了错误的请求:
cls
# https://docs.microsoft.com/en-us/graph/api/site-get-permission?view=graph-rest-1.0&tabs=http
$secret = "xxx.5i2v"
$clientid="xx-45f3-464f-xx-xx"
$tenantid="xx-x-4f1f-xx-x"
$Body = @{
'tenant' = $tenantid
'client_id' = $clientid
'scope' = 'https://graph.microsoft.com/.default'
'client_secret' = $secret
'grant_type' = 'client_credentials'
}
$Params = @{
'Uri' = "https://login.microsoftonline.com/$tenantid/oauth2/v2.0/token"
'Method' = 'Post'
'Body' = $Body
'ContentType' = 'application/x-www-form-urlencoded'
}
$AuthResponse = Invoke-RestMethod @Params
$Headers = @{
'Authorization' = "Bearer $($AuthResponse.access_token)"
'ContentType' = 'application/json'
}
# WORKS!
$Result = Invoke-RestMethod -Uri 'https://graph.microsoft.com/v1.0/sites/xx-53D2-xx-xx-xx/permissions' -Headers $Headers
$bodyTxt = $Result | ConvertTo-Json -Depth 100
write-host $bodyTxt
$body = @{
roles = @("write")
grantedToIdentities = @( @{
application = @{
displayName = "Test 7"
id = "xx-45f3-xx-aac4-xx"
}
})
}
$bodyTxt = $body | ConvertTo-Json -Depth 100
# FAILS
Invoke-WebRequest -Uri 'https://graph.microsoft.com/v1.0/sites/xx-53D2-xx-xx-xx/permissions' -Method POST -Body $body -Headers $Headers
这一定是我创建发布请求的方式。任何指向我做错了什么的指针?
【问题讨论】:
-
在最后(失败)行中,您不应该使用转换为 JSON 的正文:
-Body $bodyTxt吗?
标签: powershell microsoft-graph-api powershell-3.0