cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
oyermokhin
Regular Visitor

Using service principal for the datasource credentials

Hi!

 

I use service principal to connect from PowerBI service to Azure Data Explorer (direct query mode). Pbix is file uploaded to service via power shell. After that I get oauth2 access token for my service principal and set it to the datasource with a help of https://docs.microsoft.com/en-us/rest/api/power-bi/gateways/update-datasource#credentialdetails

Everything works fine exepct that max lifetime for the access token that I can get is 1 day. So after that my reports get unauthorized errorr. I can't find a way to provide refresh token to datasource and it is not clear if it is supported anyhow. 

 

Is there any workaround to use service principal? 

 

Thanks in advance!

2 REPLIES 2
oyermokhin
Regular Visitor

I use following method to set datasource credentials - https://docs.microsoft.com/en-us/rest/api/power-bi/gateways/update-datasource

 

$patchBody = @{ "credentialDetails" = @{ "credentials" = "{""credentialData"":[{""name"":""accessToken"",""value"":""$AccessToken""}]}" "credentialType" = "OAuth2" "encryptedConnection" = "NotEncrypted" "encryptionAlgorithm" = "None" "privacyLevel" = "Private" } }

 

 The AccessToken I get using following code

 

$requestAccessTokenUri = "https://login.microsoftonline.com/$Dataset_TenantId/oauth2/token"; $body = "grant_type=client_credentials&client_id=$Dataset_ClientId&client_secret=$Dataset_ClientSecret&resource=$KustoClusterUrl" $token = Invoke-RestMethod -Method Post -Uri $requestAccessTokenUri -Body $body -ContentType 'application/x-www-form-urlencoded'

 

This token is valid only for 1 hour, I can set it to be valid for 23h 59m using token lifetime policy, but after that my report will stop working
 
I know that other azure services also accept refresh token, so they can issues new access token automatically, but I can't provide refresh token to powerbi
 

v-jayw-msft
Community Support
Community Support

Hi @oyermokhin ,

 

Why the lifetime for the access token is only 1 day? And have you checked this document?

https://docs.microsoft.com/en-us/power-bi/developer/embedded/embed-service-principal 

 

Best Regards,

Jay

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

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

November Power BI Update 768x460.png

Check it Out!

Click here to read more about the November 2021 Updates!

M365 768x460.jpg

Microsoft 365 Collaboration Conference | December 7–9, 2021

Join us, in-person, December 7–9 in Las Vegas, for the largest gathering of the Microsoft community in the world.

Top Kudoed Authors