Share via

Graph API Powershell script to apply sensitivity labels on groups unable to patch but get works

Anonymous
2021-12-06T09:43:03+00:00

Hello, would appreciate any help.

I am running below code (app registered in AAD with appropriate permissions) I am able to run get queries for eg. get group, get labels etc. but while I try to PATCH a group to apply sensitivity label it fails with error code mentioned below.

$clientId = "1e3b8f87-09e9-4dfe-xxxxxxxxxx" $tenantName = "xxxxxxxx.onmicrosoft.com"$clientSecret = 'xxxxxxx_XXXXXXX~21-u2d5x'$ReqTokenBody = @{Grant_Type    = 'client_credentials'Scope         = 'https://graph.microsoft.com/.default'client_Id     = $clientIDClient_Secret = $clientSecret}

$TokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$TenantName/oauth2/v2.0/token" -Method POST -Body $ReqTokenBody

Create header$Header = @{Authorization = "$($TokenResponse.token_type) $($TokenResponse.access_token)"}

$labelId = "226abfea-0102-40b0-a926-8c422f81ce99"$requestBody = @{assignedLabels = @(@{labelId = $($LabelId)})}

$requestBody = $requestBody | ConvertTo-Json

$contentType = "application/json"$apiUrl = "https://graph.microsoft.com/beta/groups/e4ea79de-a11b-4cf3-a8d7-acdb42XXXXX"$assignedlable = Invoke-RestMethod -Uri $apiurl -Headers $Header -ContentType $contentType -Method PATCH -Body $requestBody

ERROR:

Invoke-RestMethod : The remote server returned an error: (401) Unauthorized.

At line:1 char:18

  • ... gnedlable = Invoke-RestMethod -Uri $apiurl -Headers $Header -ContentT ...
  •             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    
    • CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    • FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

However, if I run the below, it works

Invoke-RestMethod -Uri $apiurl -Headers $Header -Method get

Microsoft 365 and Office | Install, redeem, activate | For business | Other

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2021-12-06T11:36:32+00:00

Hi, hitendersingh

According to your description, the issue of your concern that you failed for using Graph API Powershell script to apply sensitivity labels.

Please be kindly to understand that the given issue is out of our boundary and experience.

Therefore, we would like to refer you to the right place - azure-api-management which there's a professional team dedicating into the API issues with rich experience.

Thanks for your time and your understanding would be highly appreciated.

Regards,

Zed

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful