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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
khha
New Member

Trying to get a MS refresh token for Power BI REST API authentication in PowerShell

We are using some power bi automation PowerShell script which is using a refresh token. It was working fine on the old server, but we try to migrate this script to another server. On this new server we need once a refresh token. How can I get the refresh token of a specific user account for login page of Exchange (https://outlook.office365.com)? In the technical documentation of this solution I read that I need to do it by fiddler, but I dont see the refresh token in the response header.

See below the powershell script which needs the refresh token:

 

$refresh_token_securestring = Get-Content "F:\scripts\PowerBI\ExchangeOnlineRefreshAccessToken.txt" | ConvertTo-SecureString

$refresh_token_BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($refresh_token_securestring)
$refresh_token = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($refresh_token_BSTR)
Remove-Variable refresh_token_securestring
Remove-Variable refresh_token_BSTR

# Request Access token with refresh token 

$resourceID = "https://outlook.office365.com"
$tenant_id = "MyTenantGuid"
$clientId = "MyClientIdGuid"
$uri = "https://login.microsoftonline.com/${tenant_id}/oauth2/token"
$body = @{grant_type='refresh_token';resource=$resourceID;client_id=$client_id;refresh_token=$refresh_token}
$tokenresult = Invoke-RestMethod -Method Post -Uri $uri -Body $body

 

1 ACCEPTED SOLUTION
tackytechtom
Super User
Super User

Hi @khha ,

 

The ResourceID might be the issue. Have you tried it with the ResourceID = https://graph.microsoft.com/ ?

 

Here a blog article, that helps you identifying ResourceIDs / AudienceURLs:

https://www.tackytech.blog/how-to-effectively-determine-the-token-resource-url-sometimes-also-referr...

 

 

/Tom
https://www.tackytech.blog/
https://www.instagram.com/tackytechtom/



Did I answer your question➡️ Please, mark my post as a solution ✔️

Also happily accepting Kudos 🙂

Feel free to connect with me on LinkedIn! linkedIn

#proudtobeasuperuser 

View solution in original post

1 REPLY 1
tackytechtom
Super User
Super User

Hi @khha ,

 

The ResourceID might be the issue. Have you tried it with the ResourceID = https://graph.microsoft.com/ ?

 

Here a blog article, that helps you identifying ResourceIDs / AudienceURLs:

https://www.tackytech.blog/how-to-effectively-determine-the-token-resource-url-sometimes-also-referr...

 

 

/Tom
https://www.tackytech.blog/
https://www.instagram.com/tackytechtom/



Did I answer your question➡️ Please, mark my post as a solution ✔️

Also happily accepting Kudos 🙂

Feel free to connect with me on LinkedIn! linkedIn

#proudtobeasuperuser 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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