I will be covering the following:
- Pre-requisites
- Create a master App registration and give API permission as “Application.ReadWrite.All”. This will be used to generate access token in step 3.
- How to generate access token to call Microsoft Graph Rest API.
- How to create Application Registration using MS Graph Rest API via PowerShell.
Pre-requisites
- An Azure account with an active subscription.
- Azure AD role "Application Administration" that has permission to manage applications in Azure Active Directory (Azure AD). This is not needed for running the script and if you already have master app registration with API permission as “Application.ReadWrite.All”. Next section explains how to create the master app registration.
- Azure AD tenant.
- PowerShell Installed in your local to run the scripts.
- Optionally, VS Code installed in your local computer (if you want to play around with scripts).
Create a master App registration
Generate Access Token
$body = "grant_type=client_credentials&client_id=$clientId&client_secret=<your app registration secret>&resource=https://graph.microsoft.com"
$header = @{
"Content-Type" = 'application/x-www-form-urlencoded'
}
$request = Invoke-WebRequest -Method 'Post' -Uri $url -Body $body -Header $header
Create Application using Graph API
$url = "https://graph.microsoft.com/v1.0/applications"
$header = @{
Authorization = "Bearer $token"
}
$postBody = @"
"displayName": "$DisplayName"
}
"@
try
{
$appRegistration = Invoke-RestMethod -Method 'POST' -Uri $url -Body $postBody -ContentType 'application/json' -Headers $header
}
I have created a GitHub repository that has the script to create simple app registration - CreateSimpleApplicationRegistration.ps1
https://github.com/Pujago/ApplicationRegistrationUsingMSGraphAPIs-Public
How the script works?
- CreateSimpleApplicationRegistration.ps1 will first authenticate to Azure AD (using script ConnectToAzureAD.ps1)
- Then it will generate JWT token (using script GenerateToken.ps1). This is needed for using MS Graph Rest APIs
- Once authenticated, it will create the application.
Note: To run the script locally, you will still need to login using Connect-AzAccount using master app registration credentials. Or if you have application administrator role, you login with your credentials. But the first option is preferred, if you want to automate to run the script via pipeline. The above scripts requires ConnectToAzureAD.ps1 and GenerateToken.ps1 to be in the same folder.
Please follow the steps below to use the repository:
- Go to the link: https://github.com/Pujago/ApplicationRegistrationUsingMSGraphAPIs-Public
- Clone the repository.
- Open the PowerShell terminal, go to the repository location, go to scripts folder.
- Edit the GenerateToken.ps1 and update your credentials i.e. tenant Id, client Id and secret.
Connect to Azure account:
$ServicePrincipalPW="<Master app registration client secret>"
$passwd = ConvertTo-SecureString $ServicePrincipalPW -AsPlainText -Force
Run the script:
This is the first step to create basic application. I will be writing separate blogs to update various other properties of the application like creating app roles, creating scopes, creating password credentials, setting redirect Uris, setting reply URLs, creating service principal, setting API permissions, pre-authorizing applications.
I will be creating separate PowerShell scripts for updating each app registration property, as it keeps the code neat and simple and loosely coupled.
Happy reading.
0 comments:
Post a Comment