Forum Discussion
Custom Data Connector OAuth2 refresh token not updated
mrhappy Old post but I'm sure people are still having trouble with this. I had this same issue. None of the documentation or examples discuss how to handle this.
I assume this is an issue with scheduled refreshes and not while using the desktop app. Are you using identity server for your authentication?
Our application uses IdentityServer4, so the specifics on how we fixed this issue relates to its functionality, however, should be applicable to any OAuth2 implementation.
What's happening is that when you have multiple queries using the same custom connector data source the (online) Power BI service makes all the requests asynchronously and essentially divides the requests between multiple servers. They're unaware of each other, so when your access token expires the refresh is triggered, for all of your queries, the first request to reach your server wins and is granted a new token. The next request reaches the server unaware that another request just exchanged the refresh token. The second request is then rejected for using a refresh token that has already been used.
To get around this you can do one of two things (or a combination if you don't want your refresh tokens living for extended periods of time
- Configure your authentication service to allow the reuse of refresh token
- For IdentityServer this setting is called RefreshTokenUsage on the IdentityServer4.Models.Client class
- This will allow the same refresh token to be reused each time the access token expires
- Allow a "grace period" for your refesh token before it is deemed invalid
- For IdentityServer4 you can create a custom implementation of the DefaultRefreshTokenService and override the AcceptConsumedTokenAsync method to do something like sudo: if (ConsumedTime < DateTime.UtcNow + 5) { Allow grant of new token... }
- This will prevent the server from blocking simultanious requests beating each other out in a race condition