cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Frequent Visitor

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

Accepted Solutions
Highlighted
Frequent Visitor

Re: Power BI API return 401 Unauthorized` response

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

1 REPLY 1
Highlighted
Frequent Visitor

Re: Power BI API return 401 Unauthorized` response

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

Helpful resources

Announcements
August 2020 Community Challenge: Can You Solve These?

August 2020 Community Challenge: Can You Solve These?

We're excited to announce our first cross-community 'Can You Solve These?' challenge!

Community Blog

Community Blog

Visit our Community Blog for articles, guides, and information created by fellow community members.

Upcoming Events

Upcoming Events

Wondering what events you could join or have an event to promote yourself? Check out our Upcoming Events.

Get Ready for Power BI Dev Camp

Get Ready for Power BI Dev Camp

We are thrilled to announce we will begin running a monthly webinar series named Power BI Dev Camp.