Forum Discussion
How to refresh access token with custom connector (OAuth2)?
Hi v-hashadapu rajendraongole1
I tried generating new access token from the refresh token but issue with that is i am not able to save the new access token as Credentials so that i can use the new access token instead generating it everytime.
There is no method in power query which saves the new credentials.
Also one more question when the access token or refresh token gets expired it does not ask me for re-authentication. powerbi keeps on using old token.
shared GetValidToken = () =>
let
// Fetch the stored credentials from Power BI
tokens = Extension.CurrentCredential(),
accessToken = tokens[access_token],
refreshToken = tokens[refresh_token],
expiresAtText = tokens[expires],
// expiration time (text format)
// Convert expiration time from Text to DateTime
expiresAt = DateTime.FromText(expiresAtText),
// Get the current time
currentTime = DateTime.LocalNow(),
// Check if the token has expired
isTokenExpired = expiresAt <= currentTime,
// If expired, refresh the token
newTokens =
if isTokenExpired then
let
// Call the refresh token method to get a new access token
refreshedTokens = TokenMethod("refresh_token", refreshToken),
// "dan" is placeholder for code_verifier
newAccessToken = refreshedTokens[access_token],
// New access token
newRefreshToken = refreshedTokens[refresh_token],
// New refresh token
newExpiresAt = refreshedTokens[expires]
// New expiration time
// Store the new tokens in Power BI credentials
in
Extension.CurrentCredential(
[
access_token = newAccessToken,
refresh_token = newRefreshToken,
expires = newExpiresAt
]
)
else
tokens,
// Return the existing tokens if not expired
// Return the valid access token
validAccessToken = newTokens[access_token]
in
validAccessToken;
Hi ArvindBhatt , thank you for reaching out to the Microsoft Fabric Community Forum.
rajendraongole1 provided the correct answer. Please try it. If you still have any queries related to this issue or the answer doesn't work, please share the details.
If it does, please consider accepting his answer, so others with similar queries can find it easily.
Thank you.