0

The Setup :

Am trying to access Azure PowerBI based APIs using console application since my company wants to Suspend/Resume PBI capacity to optimize costing.

Have been successful in accessing those apis as per official documentation API Emulator Window, it works by perfect as it just asks with a authentication window for username and password of my outlook account and it generates authentication token implicitly successful.

When i tried to implement calling api from my console application which will be triggered automatically scheduled, there are ways to generate authentication token programmatically. Following are the methods i used to generate the same before accessing the PBI APIs.

Approach #1 : Generating token by using Azure Active Directory Authentication Libraries..

This approach asks to add AAD authentication libraries via Nuget, upon adding the same and it goes unsuccessful as the method AcquireTokenAsync takes no parameters but tutorial specified 4 parameters to be feed into this method (function overloading missing?). So i couldnt generate token using this approach. Surely a library version problem but the official MS documentation didnt explain anything above versions available over same.

Approach #2: Authorize Active Directory without dialog box StackOverflow Question with marked as answer
This approach upon calling HTTPResponseMessage, it gets hung up without going next line or catch statement.

Approach #3: Trying to emulate token generation using POSTMAN and using the generated token on my console app to check accessibility.

This approach gives a successful token generation but when using the generated token in the console app, it says unauthorised token.

Doubt Part: Have generated ClientID, ClientSecretID and TenantID in Azure using AppRegisteration but dont know how this gets associated with PowerBI Service in azure. Do Azure Active Directory comes in place betweeen PBIService and AppRegisteration? Based on generated AppRegisteration details have tried to access this authentication api (https://login.microsoftonline.com/tenantId/oauth2/token) to generate token. This is successfully generates a token but miserably getting failed with unauthorised access on PBI api.

Am i badly missing something? will be helpful on what wrong about this concept of accessing PBI based API getting authenticated via console app. Asusual PBI community sites didnt help much.

timblistic
  • 553
  • 2
  • 10
  • 26
  • To be clear, you just want to access default PowerBi Rest api https://learn.microsoft.com/en-us/rest/api/power-bi/ not the embedded ones? – Luke Duda Jun 02 '20 at 22:22
  • Wanted to access only powerbi embedded based REST APIs https://learn.microsoft.com/en-us/rest/api/power-bi-embedded/capacities/suspend. Have mentioned the same in the first hyperlink in the question exactly. – timblistic Jun 03 '20 at 04:56

2 Answers2

2

1. How to manage Azure Power BI Embedded capacity

If you want to manage Azure Power BI Embedded capacity with rest API, please refer to the following steps.

  1. create a service principal and assign Azure RABC role to the sp(I use Azure CLI)
az login
#it will create a service principal and assign contributor role to the sp
az ad sp create-for-rbac -n "jonsp2"

enter image description here

  1. Get Token
Post https://login.microsoftonline.com/tenantId/oauth2/token
Content-Type: application/x-www-form-urlencoded

grant_type =client_credentials
&client_id=<sp app id>
&client_secret=<sp app password>
&scope=https://management.azure.com/
  1. Call Rest API
POST https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.PowerBIDedicated/capacities/{dedicatedCapacityName}/suspend?api-version=2017-10-01

Authorization: Bearer <token>

enter image description here

2. How to call Power BI rest api

If you want to call Power BI rest api, please refer to the document and the document.

The detailed steps are as below

  1. Register Azure AD application in Azure portal

  2. Configure API permissions enter image description here
    enter image description here

  3. Test (I test in postman) a. get access token enter image description here enter image description here

    b. call API enter image description here

Jim Xu
  • 21,610
  • 2
  • 19
  • 39
  • Thanks for step by step, can get your approach. The troublesome part in your guidance which am doing is that "Grant access" button during configure API permission. That button is disabled for me besides being admin account. May i know how did you enabled. – timblistic Jun 04 '20 at 07:02
  • @timblistict Could you please check you Azure AD role : https://learn.microsoft.com/en-us/azure/active-directory/users-groups-roles/directory-assign-admin-roles – Jim Xu Jun 04 '20 at 12:25
  • Thanks for correctly pointing out the role part. Have made my account as global administrator, it had me Grant Access button enabled. But token been received with null without errors. Strange thing that i have tutorial for call PBI Rest apis but have to wander around other tutorial or documentation to achieve AAD part. – timblistic Jul 03 '20 at 05:18
  • @timblistic If you want to know more about Azure AD, please refer to https://learn.microsoft.com/en-us/azure/active-directory/develop/v2-overview – Jim Xu Jul 03 '20 at 05:22
0

Suspend/Resume PowerBI API is described over here:
https://learn.microsoft.com/en-us/rest/api/power-bi-embedded/capacities/suspend
https://learn.microsoft.com/en-us/rest/api/power-bi-embedded/capacities/resume

Did you read the whole Azure REST API Reference?
All steps needed to send an HTTP request are documented over here:
https://learn.microsoft.com/en-us/rest/api/azure/

Luke Duda
  • 904
  • 9
  • 12
  • PBI API documentation been taken for development. Trickiest part is the Azure AD authorisation to create token for accessing those PBI API. – timblistic Jul 09 '20 at 07:45