Connect-ExchangeOnline Unauthorized

Abhishek Goyal 246 Reputation points
2023-03-09T04:52:52.66+00:00

I want to connect exchange online service through powershell. I use the EXO V3 for that.

I use access token as authentication mechanism. I created an app on azure active directory and give it permission of Exchange.ManageAsApp and also add this app in Exchange Administrator role. But when I connects it using command

Connect-ExchangeOnline -AccessToken $token -Organization company.onmicrosoft.com

// For creating access token I use 


$body =  @{
    Grant_Type    = “refresh_token”
    Scope         = "https://outlook.office365.com/.default"
    Client_Id     = $client_id
    Client_Secret = $secret
    Refresh_Token = $refresh_token
}

$connection = Invoke-RestMethod `
    -Uri https://login.microsoftonline.com/common/oauth2/v2.0/token `
    -Method POST `
    -Body $body

$token = $connection.access_token

but it gives me "OperationStopped: UnAuthorized" this error. While running using verbose parameter it gives me this.

VERBOSE: Returning precomputed version info: 3.1.0

VERBOSE: ModuleVersion: 3.1.0

VERBOSE: [ThreadID: #] Returning the provided AccessToken

VERBOSE: Failed to fetch banner content from server. Reason: Object reference not set to an instance of an object.

VERBOSE: ConnectionContext Removed

OperationStopped: UnAuthorized

Please help me out with it.

Exchange | Exchange Server | Development

Answer accepted by question author
Vasil Michev 127.6K Reputation points MVP Volunteer Moderator
2023-03-09T07:56:02.3666667+00:00

Use a tool such as jwt.ms to decode the token and make sure the relevant scopes are present therein. Also make sure that the corresponding service principal object has been assigned an admin role, at the least Global reader.

Was this answer helpful?

1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Anderson Lacruz 0 Reputation points
    2025-06-11T17:28:20.3833333+00:00

    Was this answer helpful?

    0 comments No comments

  2. Mohamed BEN AMOR 10 Reputation points
    2023-12-07T15:08:40.4833333+00:00

    Hello , you can follow this steps:

    Make a request to get the Token

    Import-Module MSAL.PS
    Import-Module ExchangeOnlineManagement
    $graphAppId = 'APP_ID'
    $graphAppSecret = ConvertTo-SecureString -String 'YOUR_SECRET_APP_AZURE' -AsPlainText -Force
    $tenantId = 'TENANT_ID'
    $tokenGraph = Get-MsalToken -ClientId $graphAppId -ClientSecret $graphAppSecret -TenantId $tenantId -Scopes "https://outlook.office365.com/.default"
    

    Open the connection using the Token

    Connect-ExchangeOnline -AccessToken $($tokenGraph.AccessToken) -AppId $graphAppI -Organization "YOUR_DOMAIN"
    

    Was this answer helpful?

    0 comments No comments

Your answer

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