How to Run PowerBI REST API in Azure Cloud shell

320 views Asked by At

I'm trying to get the powerbi apps details using the Azure Cloudshell,

Here is the Powershell script i'm using,

$secret="********"
$tenantId="********"
$appId="********"
    
$password= ConvertTo-SecureString $secret -AsPlainText -Force
$credential= New-Object System.Management.Automation.PSCredential ($appId, $password)
    
#Connecting to PowerBI
Connect-PowerBIServiceAccount -ServicePrincipal -Tenant $tenantId -Credential $credential

$accessToken = Get-AzAccessToken

#Get-PowerBIAccessToken
$authHeader = @{    

                    'Content-Type'='application/json'

                    'Authorization'= $accessToken.Authorization

                }

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

$allapps = (Invoke-RestMethod -Uri $uri –Headers $authHeader –Method GET).Value

$allapps.Name

When i'm running this cmd in Azure Cloudshell window, i'm getting bellow error, what could be the problem ? Code with error

I have added the SPN to the PowerBI Tenant settings and have API permission as below Azure API Permission

1

There are 1 answers

0
Rukmini On

Note that: To list the Power BI apps, service principal authentication method is not supported.

The error "Invoke-RestMethod : The remote server returned an error: (403) Forbidden" usually occurs if there are no proper permissions to perform the action.

Here, in your scenario the 403 error as you are trying to get apps using service principal authentication.

enter image description here

To resolve the issue, you can make use of below commands:

Make sure to connect to PowerBI with service account.

Connect-PowerBIServiceAccount

$apps= Invoke-PowerBIRestMethod -Url 'apps' -Method Get
$apps

enter image description here

Reference:

Apps - Get App - REST API (Power BI Power BI REST APIs)