Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Error while authenticating using registered App.

Hello,

 

As I am new to powershell coding, so I need your help in below topic :

 

While writing the below piece of powershell code to get the list of workspaces in my organization I am getting the below error :

 

PowerShell Code :

#Remember to import Exchange Online module and connect.

#$Username = "MY_USERNAME"

#$Password = ConvertTo-SecureString -String "MY_PASSWORD" -AsPlainText -Force

#$UserCredential =New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $Username, $Password

#Connect-MsolService -credential $UserCredential

#Define $info array

#$info = @()

#To edit the permission of app please access portal.azure.com in AAD

 

$clientId = "FILL ME IN"

# Calls the Active Directory Authentication Library (ADAL) to authenticate against AAD

function GetAuthToken

{

    $adal = "${env:ProgramFiles}\WindowsPowerShell\Modules\AzureRM.profile\5.8.4\Microsoft.IdentityModel.Clients.ActiveDirectory.dll"

      $adalforms = "${env:ProgramFiles}\WindowsPowerShell\Modules\AzureRM.profile\5.8.4\Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll"

    [System.Reflection.Assembly]::LoadFrom($adal) | Out-Null

    [System.Reflection.Assembly]::LoadFrom($adalforms) | Out-Null

    $redirectUri = "https://oauth.powerbi.com/PBIAPIExplorer"

    $resourceAppIdURI = "https://analysis.windows.net/powerbi/api"

    $authority = "https://login.microsoftonline.com/common/oauth2/authorize";

    $authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authority

    $authResult = $authContext.AcquireToken( $resourceAppIdURI, $clientId, $redirectUri, "Auto")

    return $authResult

}

# Get the auth token from AAD

$token = GetAuthToken

# Building Rest API header with authorization token

$authHeader = @{

   'Content-Type'='application/json'

   'Authorization'=$token.CreateAuthorizationHeader()

}

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12

#Lets get all Groups

$uri = "https://api.powerbi.com/v1.0/myorg/groups"

$groups = Invoke-RestMethod -Uri $uri -Headers $authHeader -Method GET -Verbose

$groups.value

 

Error Message while running the script :

Error.png

 

 

Although, in app settings redirect URI and resource APP ID uri are pointing to the respective links.

Also, I am following the below links as reference:

Register an app to embed Power BI content in a Power BI embedded analytics application - Power BI | ...

Power BI REST APIs for embedded analytics and automation - Power BI REST API | Microsoft Docs

Using the Power BI API with PowerShell scripts - Refreshing your dataset - Reporting/Analytics Made ...

 

Thank you in advance

1 ACCEPTED SOLUTION
V-lianl-msft
Community Support
Community Support

Hi @Anonymous ,

 

The resource must be either the client ID of the app or if you want to specify the resource parameter in form of URL, you need to Expose an API on the application and add required scopes. The Resource URL in the request must match with the Application ID URI. Please refer to the highlighted sections in the screenshot below:

test.png

Once this is done, you need to add permissions for that API under API permissions section of the application and grant admin consent.


Best Regards,
Liang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
V-lianl-msft
Community Support
Community Support

Hi @Anonymous ,

 

The resource must be either the client ID of the app or if you want to specify the resource parameter in form of URL, you need to Expose an API on the application and add required scopes. The Resource URL in the request must match with the Application ID URI. Please refer to the highlighted sections in the screenshot below:

test.png

Once this is done, you need to add permissions for that API under API permissions section of the application and grant admin consent.


Best Regards,
Liang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Thank you @V-lianl-msft  for providing the solution.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.