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

July 28 - August 9 | Final Round of the Power BI Dataviz World Championships. This is your chance. Learn more

Reply
MSchultz93
Helper I
Helper I

Rest API Oauth2 as a data source

I'm relatively new to Power BI. I have a question regarding the best practice of the OAuth2 flow using a Rest API. I want to do the following,

1. Automatically update the refresh token and access token

2. Use the access token to paginate data from the rest API.

I have written the following function that retrieves a new refresh token and access token in the advanced editor. However, I need to manually put the refresh token into the function. Can someone give me advice on how I should set it up?

() =>
let
grant_type = grant_type,
refresh_token="1111122222333333",
client_id = client_id,
client_secret = client_secret,
requestBody = "grant_type=" & grant_type & "&refresh_token=" & refresh_token & "&client_id=" & client_id & "&client_secret=" & client_secret,
options = [ Headers = [ #"Content-Type" = "application/x-www-form-urlencoded" ],
Content = Text.ToBinary(requestBody) ],
response = Web.Contents("https://auth.happyapi123.com/oauth/token", options),
json = Json.Document(response)
in
json

Here is the code that I have written, note I've hidden private information. grant_type, client_id and client_secret are parameters

I am not sure how to proceed, or if I should tackle it in a different way

2 REPLIES 2
amitchandak
Super User
Super User

@MSchultz93 , refer if this can help

 

Rest source - Token based
https://www.advaiya.com/generate-dynamic-token-and-get-data-from-custom-api-in-power-bi/

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Hi, thank you for the reply!

I actually built my code from that link, however, on that website an access token is queried from a static URL with a username and password. The function never needs to change. My logic is more complicated. 

I have a query that is using a refresh token to obtain an access token and a refresh token. The new refresh token must be used the next time I generate an access token. Therefore the query that creates the access token must use the last created refresh token and therefore the query is not static like the one in the link.

Do you have any advice on that? If you need any clarification let me know

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

Fabric Community Sticker Design Challenge Barcelona Carousel

Fabric Community Sticker Challenge - Barcelona 2026

If you love stickers, then you will definitely want to check out our community sticker challenge, Barcelona edition!

July Power BI Update Carousel

Power BI Monthly Update - July 2026

Check out the July 2026 Power BI update to learn about new features.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Top Solution Authors