Forum Discussion
How to refresh access token with custom connector (OAuth2)?
Hi ArvindBhatt , thank you for reaching out to the Microsoft Fabric Community Forum.
Yes as rajendraongole1 said Power BI does not automatically refresh the access token once it expires. You will need to manually refresh the token before making API calls. Here's how you can do it:
- When the user logs in, save the refresh token provided by the API.
- Use the refresh token to request a new access token. This request should be sent to the API's token endpoint using the POST method.
- Replace the old access token with the new one received from the API.
Example in M code:
let
refreshToken = "your_refresh_token_here",
clientId = "your_client_id_here",
clientSecret = "your_client_secret_here",
tokenEndpoint = "https://api.example.com/oauth2/token",
body = "grant_type=refresh_token&refresh_token=" & refreshToken & "&client_id=" & clientId & "&client_secret=" & clientSecret,
response = Web.Contents(tokenEndpoint, [Headers=[#"Content-Type"="application/x-www-form-urlencoded"], Content=Text.ToBinary(body)]),
newAccessToken = response[access_token]
in
newAccessToken
If this helps, please consider marking it 'Accept as Solution' so others with similar queries may find it more easily. If not, please share the details.
Thank you.