Forum Discussion
Power BI Rest API wont accept Application Token
- Anonymous4 years ago
Solution found thanks to SpartaBI !
1. Go to App registration -> New registration
2. Give your app a name and use the default settings
3. Create a secret, and make sure you copy the right value
4. Copy the ClientId & TenantIdNo need to do anything else with the app config.
Just add "MyNewApp" to the AAD security group, wait for a couple of minutes, then try your code.Then for running a Non-Admin API call, the App needs added into the Workspace Permissions you want to read from.
You do not need to add any delegated permissions to the app.
In order to allow a service principal access no non-admin APIs, it must have the same dataset/workspace permissions as any other user. For example- you can add the service principal as a member of a workspace.This is exactly the purpose of the other setting in the Admin Portal.
It defines the service principals whose permissions are being managed by the Power BI Service itself.
Hi Anonymous,
You manage to use a Power BI Admin API from Powershell, but fail to run the same call from PQ?
Can you share your code?
The Web.Contents doc lists a "ManualStatusHandling" parameter.
I believe you can use it to specify {401} in order to receive the actual error message.
- Anonymous4 years agoNot applicable
Just to clarify something, the way I used to get my access token when doing this more manually, before trying to automate by using the Registered App, I went to the Microsoft Documents site for the Power BI APIs. You used to be able to click Try It and in that window you could copy out your Access Token. Microsoft for some reason removed that part of the website and you can't Try It anymore. I wasn't having success getting a good token either from Postman, so I went the PowerShell route just for the manual token so I can at least use the report yet while trouble shooting this Registered App token usage.
PowerShell was simple, just made sure to have the cdmlets, #1 logged in (didn't matter if ServiceAccount or personal Login), and then ran the #2 to Get Token
- Connect-PowerBIServiceAccount
- Get-PowerBIAccessToken -AsString
Parameter values that are not my sensitive data are:
grant_type = client_credentials
resource = https://analysis.windows.net/powerbi/api
This is the M-code getting the Token from the Registered App
() =>
let
body = "grant_type=" & grant_type & "&client_id=" & client_id & "&client_secret=" & client_secretvalue & "&resource=" & resource,
Data = Json.Document(Web.Contents("https://login.microsoftonline.com/" & tenant_id & "/oauth2/token/", [Headers=[#"Content-Type"="application/x-www-form-urlencoded"], Content=Text.ToBinary(body)])),
access_token = Data[access_token]
in
access_tokenThis is the M-code where I am running one of the API calls. *Notice where I have the CurrentToken parameter being used, which uses the PowerShell produced Token. Once I remove that and use the GET Access Token Function, it won't work. I have even tested invoking the Function and pasting the result into the CurrentToken and didn't make a difference
let
Source = Json.Document(Web.Contents("https://api.powerbi.com/v1.0/myorg/",
[
RelativePath = "admin/groups?$top=5000&$expand=datasets,dataflows,reports,dashboards,users",
Headers=[Authorization="Bearer " & CurrentToken/*#"GET Access Token"()*/]
] )),
value = Source[value],
#"Converted to Table" = Table.FromList(value, Splitter.SplitByNothing(), null, null, ExtraValues.Error)
in
#"Converted to Table"