Granting app registrations permissions to site

Viewed 98

I am trying to do this from 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"
    }
  }]
}

It works when I want to list permissions. However when I want to do a post I get an 400 bad request. I expect it to be the way I generate the body? Anyone has succeeded in granting permissions with graph API and 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

When I look in the bodyTxt it looks like this. Not sure if that can be the problem (the hash table part): enter image description here

I tried doing it from postman and that works: enter image description here

1 Answers

You have to add the -Depth parameter

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

Source.

ConvertTo-Json -Depth 100
Related