Last active
February 5, 2021 11:13
-
-
Save rudfoss/096465e58da4b10e820aa2fb9d602fd4 to your computer and use it in GitHub Desktop.
A small script demonstrating how to authenticate with MS Graph and update redirect uris for an application.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
This script demonstrates how to authenticate with the Microsoft Graph and update the redirect uris for an app registration | |
Requirements: | |
1. Register an application in the tenant (management app) | |
2. Grant the app the following API permissions: | |
- Application.ReadWrite.All | |
.PARAMETER MgmtAppId | |
The ID of the management app which has the proper API permission scopes to perform the action. | |
.PARAMETER MgmtAppClientSecret | |
The client secret for the management app | |
.PARAMETER TenantId | |
The tenant id for the Azure AD/B2C tenant where the apps are stored | |
.PARAMETER AppId | |
The app id of the application where redirect uris should be modified | |
.PARAMETER RedirectUris | |
Specify the redirect uris as a string array. | |
#> | |
param ( | |
[Parameter(Mandatory)] | |
[string] | |
$MgmtAppId, | |
[Parameter(Mandatory)] | |
[string] | |
$MgmtAppClientSecret, | |
[Parameter(Mandatory)] | |
[string] | |
$TenantId, | |
[Parameter(Mandatory)] | |
[string] | |
$AppId, | |
[Parameter(Mandatory)] | |
[string[]] | |
$RedirectUris | |
) | |
$graphBaseUrl = "https://graph.microsoft.com/v1.0/" | |
$loginUrl = "https://login.microsoftonline.com/$TenantId/oauth2/v2.0/token" | |
$body = @{ | |
client_id = $AppId | |
client_secret = $ClientSecret | |
scope = "https://graph.microsoft.com/.default" | |
grant_type = "client_credentials" | |
} | |
Write-Host "Authenticating with MS Graph" | |
$authResponse = Invoke-RestMethod ` | |
-Method Post ` | |
-Uri $loginUrl ` | |
-Body $body ` | |
-ContentType "application/x-www-form-urlencoded" | |
$tokenType = $authResponse.token_type | |
$accessToken = $authResponse.access_token | |
$restHeader = @{ | |
Authorization = "$tokenType $accessToken" | |
} | |
$redirectUrisBody = "{`"web`":{`"redirectUris`":[`"$($RedirectUris -join '","')`"]}}" | |
Write-Host "Update application $AppId with new redirect urls" | |
$patchResponse = Invoke-RestMethod ` | |
-Uri "$graphBaseUrl/applications/$AppObjectId" ` | |
-Headers $restHeader ` | |
-ContentType "application/json" ` | |
-Method Patch ` | |
-Body $redirectUrisBody |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment