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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
marcio_fornari
Resolver I
Resolver I

Power BI API return 401 Unauthorized` response

Hello Everybody, 

I have a problem with a Power BI API

 

First i get the Token:

 

 

$url = 'https://login.windows.net/' . $directoryIdAzure . '/oauth2/token'
            /** @var GuzzleHttp\Client $client **/
            $response = $client->post(
                $url,
                [
                    "headers" => [
                        "Accept" => "application/json"
                    ],
                    'form_params' => [
                        'resource'      => 'https://analysis.windows.net/powerbi/api',
                        'client_id'     => $clientIdAzure,
                        'client_secret' => $clientSecretAzure,
                        'grant_type'    => 'client_credentials',
                        'username'      => $user,
                        'password'      => $pass,
                        'scope'         => 'openid',
                    ]
                ]
            );

            $body = json_decode($response->getBody()->getContents(), true);

            $token = $body['access_token'];

 

After i need list all Workspaces using a token

 

 

            $client2 = new \GuzzleHttp\Client();
            $res = $client2->request(
                'GET',
                'https://api.powerbi.com/v1.0/myorg/groups',
                [
                    'headers' =>
                    [
                        'Authorization' => 'Bearer ' . $token,
                        'Accept' => 'application/json',
                        'Content-type' => 'application/json'
                    ]
                ]
            );

            $workspaces = json_decode($res->getBody()->getContents(), true);

 

Return this error for me:

 

"Client error: `GET https://api.powerbi.com/v1.0/myorg/groups` resulted in a `401 Unauthorized` response"

 

The permissions in the Azure it's OK:

 

 

 

1 ACCEPTED SOLUTION
marcio_fornari
Resolver I
Resolver I

I Solved my problem.

 

I change 

'grant_type'    => 'client_credentials',

 

to 

 

'grant_type'    => 'password',

 

and I had to disable Security Defaults in the Azure 

Azure Portal > Azure AD > Properties > Click on Manage Security Defaults link > Toggle Enable Security Defaults button to NO.

 

I hope thats help anyone. 

View solution in original post

2 REPLIES 2
spatialcase
New Member

You also need to enable use of the Fabric API within the Developer settings of your Power BI tenant.  See ths link for details:  https://learn.microsoft.com/en-us/power-bi/developer/embedded/embed-service-principal

 

marcio_fornari
Resolver I
Resolver I

I Solved my problem.

 

I change 

'grant_type'    => 'client_credentials',

 

to 

 

'grant_type'    => 'password',

 

and I had to disable Security Defaults in the Azure 

Azure Portal > Azure AD > Properties > Click on Manage Security Defaults link > Toggle Enable Security Defaults button to NO.

 

I hope thats help anyone. 

Helpful resources

Announcements
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 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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