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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

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
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.