Post

Client ID and Secret Authentication to Microsoft Graph API

Demonstrating Client ID and Secret Authentication to Microsoft Graph

Client ID and Secret Authentication to Microsoft Graph API

Authenticating to Microsoft Graph API

In this post, I’ll demonstrate how to authenticate to the Microsoft Graph API using Client ID and Secret in PowerShell. The client secret method is suitable for server-to-server communication where a client secret is used to authenticate the application.

First off we need to create the app registration in Azure/Entra

Go to App Registrations page and click create “New registration” img-description

Give it a new and click “Register” img-description

On the front page of the new app also called “Overview” we need to get the “Directory (tenant) ID” and “Application (client) ID” img-description

Go to the “Certificates & secrets” page and click “New client secret” img-description

Now you should see your new secret (Save this for later as it will not always be visible on the app registration) img-description

Now lets take what we just created and ask for a token

First off we need to define or variables for authentication take the tenant id, client id and the secret we made above in our instance it would be

1
2
3
4
# Tenant ID, Client ID, and Client Secret for the MS Graph API
$tenantId = "Your-tenant-id"
$clientId = "Your-client-id"
$clientSecret = "Your-Secret"

Next up we need to configure our body for authenticaton. By looking at Microsoft documentation We can see that we need to provide the following information in our body:

ParameterConditionDescription
tenantRequiredYour tenant id
client_idRequiredYour client id
scopeRequiredYou only need to specify anything if you are not going to hit the default endpoint
client_secretRequiredYour client secret
grant_typeRequiredclient_credentials

Right now that we have an understanding of what the endpoint expects then we can form our body from that like shown here:

1
2
3
4
5
6
7
# Default Token Body
$tokenBody = @{
    Grant_Type    = "client_credentials"
    Scope         = "https://graph.microsoft.com/.default"
    Client_Id     = $clientId
    Client_Secret = $clientSecret
}

Easy as that now all we need is to send a request to Azure/Entra for the token on the endpoint/host specified in the documentation “https://login.microsoftonline.com/tenantId/oauth2/v2.0/token”

1
2
3
4
# Request a Token
$tokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token" -Method POST -Body $tokenBody
# Output the token
$tokenResponse

And you are done now you have a token you can use to make api request to microsoft graph if you need assitance as to how, you can see one of my other blog posts.

Complete Script with Synopsis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<#
.SYNOPSIS
    Demonstrates how to authenticate using a client secret with Microsoft Graph API.

.DESCRIPTION
    This script shows how to authenticate an application using a client secret.
    It requests an access token using the client credentials flow.

.NOTES
    MS Docs on how to use Ms Graph API with client credentials flow:
    https://learn.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-client-creds-grant-flow

.PARAMETER tenantId
    The tenant ID of the Azure AD tenant.

.PARAMETER clientId
    The client ID of the registered application.

.PARAMETER clientSecret
    The client secret of the registered application.

.EXAMPLE
    # Set environment variables for tenantId, clientId, and clientSecret
    $env:tenantId = "your-tenant-id"
    $env:clientIdSecret = "your-client-id"
    $env:clientSecret = "your-client-secret"

    # The script will output the access token.
#>

# Tenant ID, Client ID, and Client Secret for the MS Graph API
$tenantId = $env:tenantId
$clientId = $env:clientId2
$clientSecret = $env:clientSecret

# Default Token Body
$tokenBody = @{
    Grant_Type    = "client_credentials"
    Scope         = "https://graph.microsoft.com/.default"
    Client_Id     = $clientId
    Client_Secret = $clientSecret
}

# Request a Token
$tokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token" -Method POST -Body $tokenBody

# Output the token
$tokenResponse
This post is licensed under CC BY 4.0 by the author.

Trending Tags