向网站授予应用注册权限

时间:2021-07-16 09:08:08

标签: azure powershell sharepoint microsoft-graph-api

我正在尝试从 PowerShell 执行此操作: https://docs.microsoft.com/en-us/graph/api/site-post-permissions?view=graph-rest-1.0&tabs=http

POST https://graph.microsoft.com/v1.0/sites/{sitesId}/permissions
Content-Type: application/json

{
  "roles": ["write"],
  "grantedToIdentities": [{
    "application": {
      "id": "89ea5c94-7736-4e25-95ad-3fa95f62b66e",
      "displayName": "Contoso Time Manager App"
    }
  }]
}

当我想列出权限时它有效。但是,当我想发帖时,我收到了 400 个错误的请求。我希望它是我生成身体的方式?有没有人成功通过图形 API 和 PowerShell 授予权限?

$secret = "mk1_UVNxxbD991D.5i2v"
$clientid="4fcxxd9f9"
$tenantid="57fxxb68f"


$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)"
}

# WORKS!
$Result = Invoke-RestMethod -Uri 'https://graph.microsoft.com/v1.0/sites/8xx0A0/permissions' -Headers $Headers
write-host $Result


$body = @{
    roles               = @('Write')
    grantedToIdentities = @( @{
            application = @{
                id          = '4fc5dxx9f9'
                displayName = 'KK test 7'
            }
        })
}

$bodyTxt = $body | ConvertTo-Json

# FAILS
$Result = Invoke-RestMethod -Uri 'https://graph.microsoft.com/v1.0/sites/810Axx5F0A0/permissions' -Headers $Headers -Body $body -Method Post
write-host $Result

当我查看 bodyTxt 时,它看起来像这样。不确定这是否可能是问题(哈希表部分): enter image description here

我尝试从邮递员那里做这件事,效果很好: enter image description here

1 个答案:

答案 0 :(得分:0)

您必须添加 -Depth 参数

<块引用>

指定包含对象的级别数 JSON 表示

Source.

ConvertTo-Json -Depth 100