azure - 向网站授予应用程序注册权限

标签 azure powershell sharepoint microsoft-graph-api

我正在尝试从 PowerShell 执行此操作: https://learn.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 错误请求。我希望这就是我生成 body 的方式?有人成功使用图形 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

我尝试从 postman 那里做这件事并且有效: enter image description here

最佳答案

您必须添加-Depth参数

Specifies how many levels of contained objects are included in the JSON representation

Source.

ConvertTo-Json -Depth 100

关于azure - 向网站授予应用程序注册权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68406416/

相关文章:

reactjs - 将应用程序从react-azure-adb2c移植到react-adal

c# - ITransformableFilterValues 实现,用于过滤包含 ParameterValues 的条目

database - 用于在特定记录处打开 Access Web DB 表单的 URL

linux - VM 指标 - Azure - 如何检查单个进程的 CPU 和内存利用率?

azure - 如何使用我的 AAD 凭据登录到我的 Azure VM?

python - 在 Python 中获取图像对象大小(以字节为单位)- Azure 中的 BadRequest 错误

powershell - 带 powershell 的 Graphite

powershell - bat 文件为 win server 2012 安装 .net 3.5 框架

powershell - TFS 构建 : Run a Powershell script as administrator

sharepoint - 在不使用 infopath 的情况下创建表单以在 SharePoint 中发送电子邮件的简单方法