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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

How to implement getNewUserAccessToken() function for PowerBI embedded

On Microsoft docs you can find an example how to refresh the powerbiembedded token to overcome the 1 hour lifetime.

Refresh the access token in Power BI embedded analytics | Microsoft Docs

 

However this note seems to be the key to the solution

===========================

Note

getNewUserAccessToken() is a function you need to implement, it calls your application back-end for generating a new embed token, or refreshes the Azure AD token.

===========================

 

I tried several things to create that getNewUserAccessToken() function, but don't know how to do it and cannot find the right solution until now.

Is there anyone who already has successfull implemented this?

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @v-yiruan-msft , I have created the PowerBI embedded by using creating a portal using the Power Platform. There I used the procedure mentioned here: Set up Power BI integration - Power Apps | Microsoft Docs

 

It was not possible to create the refresh token, because the token aquisition is done by the Power Platform.

 

However it was possible to extend the tokenlifetime by creating an Azure AD policy and assign it to the Power BI service account. I was struggling with it because I tried to extend the tokenlifetime of the PowerApp account that was registered in my tenant, that didn't solve the issue. The embedded PowerBI session still expired after hour.

 

Below the PowerShell script that I have used to extend the token lifetime to 23 hours and it works as expected.

 

 

# Login
Connect-AzureAD
Get-AzureADServicePrincipal -filter "DisplayName eq 'Power BI Service'" -Verbose

#Define policy for 23 hours
$policy = New-AzureADPolicy -Definition @('{"TokenLifetimePolicy":{"Version":1,"AccessTokenLifetime":"23:00:00"}}') -DisplayName "PowerBI-access-token-Policy" -IsOrganizationDefault $false -Type "TokenLifetimePolicy"

# Get ID of the service principal
$sp = Get-AzureADServicePrincipal -Filter "DisplayName eq 'Power BI Service'"

# Assign policy to the id of the service principal found in the previous step
Add-AzureADServicePrincipalPolicy -Id $sp.ObjectId -RefObjectId $policy.id

 

 

 

View solution in original post

5 REPLIES 5
mholloway
Frequent Visitor

The reply marked as the solution doesn't do what the OP asked for -- refreshing the access token. It's a different approach, extending the lifetime of the original access token. It's frustrating coming to this page looking for help on how to refresh the access token, and the 'solution' is something totally different -- and something that doesn't work in all situations. 

 

I'd encourage moderators to not allow marking a question 'solved' when it has not, in fact, been solved. The answer is at best a workaround that achieves the same goal, without ever answering the original question. The OP asked for how to implement getNewUserAccessToken(), specifically. The 'answer' is something else entirely.

Chorda
Frequent Visitor

Hi @v-yiruan-msft @Anonymous 

 

I've created in another post a related but different topic you could give me a hand with and I'd make you a monument!

 

Here is the post:

https://community.powerbi.com/t5/Developer/Refresh-the-token-in-PowerBI-Embedded/m-p/2548512#M36427

 

v-yiruan-msft
Community Support
Community Support

Hi @Anonymous ,

Please review the content in the following links, hope they can help you.

1. Get access token by postman or programming with C#

REST API Get Access Token

yingyinr_0-1635757519611.png

2. Get an Azure AD access token for your Power BI application

3. Get access token by Phython

access token power bi python

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

Hi @v-yiruan-msft , I have created the PowerBI embedded by using creating a portal using the Power Platform. There I used the procedure mentioned here: Set up Power BI integration - Power Apps | Microsoft Docs

 

It was not possible to create the refresh token, because the token aquisition is done by the Power Platform.

 

However it was possible to extend the tokenlifetime by creating an Azure AD policy and assign it to the Power BI service account. I was struggling with it because I tried to extend the tokenlifetime of the PowerApp account that was registered in my tenant, that didn't solve the issue. The embedded PowerBI session still expired after hour.

 

Below the PowerShell script that I have used to extend the token lifetime to 23 hours and it works as expected.

 

 

# Login
Connect-AzureAD
Get-AzureADServicePrincipal -filter "DisplayName eq 'Power BI Service'" -Verbose

#Define policy for 23 hours
$policy = New-AzureADPolicy -Definition @('{"TokenLifetimePolicy":{"Version":1,"AccessTokenLifetime":"23:00:00"}}') -DisplayName "PowerBI-access-token-Policy" -IsOrganizationDefault $false -Type "TokenLifetimePolicy"

# Get ID of the service principal
$sp = Get-AzureADServicePrincipal -Filter "DisplayName eq 'Power BI Service'"

# Assign policy to the id of the service principal found in the previous step
Add-AzureADServicePrincipalPolicy -Id $sp.ObjectId -RefObjectId $policy.id

 

 

 

Hi @Anonymous ,

It's glad to hear that your problem has been resolved. And thanks a lot for sharing the solution here, it will help the others in the community get the solution easily if they face the same problem with you. Much appreciated!

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Kudoed Authors