0
votes

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.

2
To be clear, you just want to access default PowerBi Rest api docs.microsoft.com/en-us/rest/api/power-bi not the embedded ones? - Luke Duda
Wanted to access only powerbi embedded based REST APIs docs.microsoft.com/en-us/rest/api/power-bi-embedded/capacities/…. Have mentioned the same in the first hyperlink in the question exactly. - timblistic

2 Answers

1
votes

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

0
votes

Suspend/Resume PowerBI API is described over here:
https://docs.microsoft.com/en-us/rest/api/power-bi-embedded/capacities/suspend
https://docs.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://docs.microsoft.com/en-us/rest/api/azure/