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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
Anonymous
Not applicable

OAuth 2.0 refresh token changes each time

Hi,

I'm hoping someone can help me... I'm trying to connect Power BI to the Xero API to pull some details.

The API has two tokens:

  • Access Token - valid for 30 minutes;
  • Refresh Token - valid for a single use.

I've created a parameter for the Refresh Token, and I can use it in a Power Query to successfully connect to the API, get an Access Token and get the data that I'm interested in. However, in performing this query the Refresh Token is replaced by a new Refresh Token which needs to be stored somewhere to be used next time I hit refresh.

So, what are the options for saving my updated Refresh Token, and having it available to use next time? Can Power Query take the value and pass it back to update the Parameter?

I've pasted the query below:

 

let
 // Concatenates the Key & Secret and converts to base64
 authKey = "Basic " & Binary.ToText(Text.ToBinary(#"API Key" & ":" & #"API Secret"),0),
 url = #"Token URL",
 // Uses the Xero POST oauth2/token method to obtain a bearer access token and new refresh token
 GetJson = Web.Contents(url,
     [
         Headers = [#"Authorization"=authKey,
                    #"Content-Type"="application/x-www-form-urlencoded;charset=UTF-8"],
         Content = Text.ToBinary("grant_type=refresh_token&refresh_token=" & #"Refresh Token") 
     ]
 ),
 FormatAsJson = Json.Document(GetJson),
 // Gets token from the Json response
 #"RefreshToken" = FormatAsJson[refresh_token], //This is the updated Refresh Token that I need to save somewhere
 AccessToken = FormatAsJson[access_token],
 AccessTokenHeader = "bearer " & AccessToken,
 // Uses the Xero Project GET method using the bearer token from the previous POST oauth2/token method
 GetJsonQuery = Web.Contents(#"Projects URL",
     [
         Headers = [#"Authorization"=AccessTokenHeader,
                    #"xero-tenant-id"="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"]
     ]
 ),
FormatAsJsonQuery = Json.Document(GetJsonQuery),
    items = FormatAsJsonQuery[items],
    #"Converted to Table" = Table.FromList(items, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"projectId", "contactId", "name"})
in
    #"Expanded Column1"

 

 Any assistance on how to grab the updated refresh token, and push it back to the parameter, or alternatively any other option to store my refresh token somewhere else (not a parameter) and use it in the query and have the query update it, would be greatly appreciated.

Cheers,

Andy

5 REPLIES 5
mfgbarro
Regular Visitor

Hi,

 

It's been four years...   is there a way to do this now??   (to update the value of a parameter after refreshing the token through the code below?)

 

Thanks.

Anonymous
Not applicable

Hi @Anonymous 

i want to dynamically access token and refresh token in power Query(M Code)

From Xero API

 

Can you send me demo file please....

 

Anonymous
Not applicable

Hi @Anonymous 

Did you find any solution? I want to do the same. 

artemus
Microsoft Employee
Microsoft Employee

If you want to do this you will need to build your own custom connector and implement the Refresh function.

 

See here to get started:

https://docs.microsoft.com/en-us/power-query/startingtodevelopcustomconnectors

lbendlin
Super User
Super User

Excellent request, unfortunately not currently supported in Power Query. Definitely raise an idea for that!

 

For now you need to re-authenticate each time you want to do a refresh.

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors
Top Kudoed Authors