Invalid JWT access token

Flipping 20 Reputation points
2025-03-20T13:50:58+00:00

Hello everyone,

I'm trying to run a PowerShell script in an Azure Automation runbook to connect to Microsoft Graph, but I'm encountering an issue with the connection. Specifically, I keep getting the error: invalid JWT access token.

Can anyone tell me what i am doing wrong?

It works in PowerShell:

Define App Registration details

$tenantId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

$clientId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

$clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Client secret value

Define the resource for Azure Management API

$scope = "https://graph.microsoft.com/.default"

$tokenUrl = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"

Get authentication token using client credentials flow

$body = @{

grant_type    = "client_credentials"

client_id     = $clientId

client_secret = $clientSecret

scope         = $scope

}

$response = Invoke-RestMethod -Method Post -Uri $tokenUrl -ContentType "application/x-www-form-urlencoded" -Body $body

$accessToken = $response.access_token

Validate token retrieval

if (-not $accessToken) {

    Write-Host "Failed to obtain access token" -ForegroundColor Red

    exit

}



Write-Host "Access Token obtained successfully" -ForegroundColor Green

# Convert the access token to a SecureString

$secureAccessToken = ConvertTo-SecureString $accessToken -AsPlainText -Force

Connect to Microsoft Graph using the SecureString access token

Connect-MgGraph -AccessToken $secureAccessToken
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
24,342 questions
0 comments No comments
{count} votes

Accepted answer
  1. SrideviM 2,620 Reputation points Microsoft External Staff
    2025-03-24T09:23:37.5333333+00:00

    Hello Flipping ,

    I understand you're encountering the "Invalid JWT access token" error when attempting to connect to Microsoft Graph within your Azure Automation runbook.

    This is a known issue that arises when using version 2.26.1 of the Microsoft.Graph.Authentication module with PowerShell 7.2.

    I have one Azure Automation account where Microsoft Graph modules installed with 2.26.1 version:

    enter image description here

    When I ran your script in PowerShell 7.2 runbook, I too got same error as below:

    enter image description here

    To resolve this, you need to revert back its previous version 2.25.0 as a workaround.

    In my case, I deleted the existing 2.26.1 Microsoft Graph modules as below:

    enter image description here

    Now, I ran below PowerShell script in Azure Cloud Shell to install 2.25.0 version Microsoft Graph modules:

    
    # Import Microsoft.Graph.Authentication module
    
    $moduleName = 'Microsoft.Graph.Authentication'
    
    $moduleVersion = '2.25.0'
    
    New-AzAutomationModule -AutomationAccountName 'AutAccName' -ResourceGroupName 'rgName' -Name $moduleName -ContentLinkUri "https://www.powershellgallery.com/api/v2/package/$moduleName/$moduleVersion" -RuntimeVersion '7.2'
    
    # Import Microsoft.Graph.Users module
    
    $moduleName = 'Microsoft.Graph.Users'
    
    $moduleVersion = '2.25.0'
    
    New-AzAutomationModule -AutomationAccountName 'AutAccName' -ResourceGroupName 'rgName' -Name $moduleName -ContentLinkUri "https://www.powershellgallery.com/api/v2/package/$moduleName/$moduleVersion" -RuntimeVersion '7.2'
    

    Response:

    enter image description here

    Make sure to wait for few minutes until the modules status turns "Available" as below:

    enter image description here

    When I ran the PowerShell script again after reverting back to 2.25.0 version, I got the response successfully as below:

    
    # Define App Registration details
    
    $tenantId = "tenantId"
    
    $clientId = "appId"
    
    $clientSecret = "secret" # Client secret value
    
    # Define the resource for Azure Management API
    
    $scope = "https://graph.microsoft.com/.default"
    
    $tokenUrl = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"
    
    # Get authentication token using client credentials flow
    
    $body = @{
    
        grant_type    = "client_credentials"
    
        client_id     = $clientId
    
        client_secret = $clientSecret
    
        scope         = $scope
    
    }
    
    try {
    
        $response = Invoke-RestMethod -Method Post -Uri $tokenUrl -ContentType "application/x-www-form-urlencoded" -Body $body
    
        $accessToken = $response.access_token
    
        # Validate token retrieval
    
        if (-not $accessToken) {
    
            Write-Host "Failed to obtain access token" -ForegroundColor Red
    
            exit
    
        }
    
        Write-Host "Access Token obtained successfully" -ForegroundColor Green
    
        # Convert the access token to a SecureString
    
        $secureAccessToken = ConvertTo-SecureString $accessToken -AsPlainText -Force
    
        # Connect to Microsoft Graph using the SecureString access token
    
        Connect-MgGraph -AccessToken $secureAccessToken
    
        Write-Host "Connected to Microsoft Graph successfully" -ForegroundColor Green
    
    }
    
    catch {
    
        Write-Host "Error: $_" -ForegroundColor Red
    
    }
    
    Import-Module Microsoft.Graph.Users
    
    Get-MgUser -Top 10 | Select-Object DisplayName, Id
    
    

    Response:

    enter image description here

    To know more regarding this known issue, you can refer this GitHub issue.

    Hope this helps!


    If this answer was helpful, please click "Accept the answer" and mark Yes, as this can be beneficial to other community members.

    User's image

    If you have any other questions or still running into more issues, let me know in the "comments" and I would be happy to help you.

    3 people found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. veiganlime 5 Reputation points
    2025-03-21T12:43:50.78+00:00

    Hello Flipping,

    I faced the same issue after updating the Microsoft.Graph.Application and Microsoft.Graph.Authentication modules to version 2.26.1. Manually downgrading these modules to version 2.22.0 resolved the issue, allowing me to use the ‘Connect-MgGraph’ command again.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.