Last active
August 29, 2015 13:56
-
-
Save wadewegner/8834571 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private async void btnExpireToken_Click(object sender, RoutedEventArgs e) | |
{ | |
_token.AccessToken = "GARBAGE"; | |
var response = await RetryMethod<dynamic>(GetAccounts, 3, 0, RefreshToken); | |
} | |
private async Task RefreshToken() | |
{ | |
var auth = new AuthenticationClient(); | |
await auth.TokenRefreshAsync(ConsumerKey, _token.RefreshToken); | |
_token.AccessToken = auth.AccessToken; | |
} | |
private async Task<dynamic> GetAccounts() | |
{ | |
var client = new ForceClient(_token.InstanceUrl, _token.AccessToken, "v29.0"); | |
var accounts = await client.QueryAsync<dynamic>("SELECT id, name, description FROM Account"); | |
return accounts; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<T> RetryMethod<T>(Func<Task<T>> method, int numRetries, int retryTimeout, Func<Task> onInvalidTokenAction) | |
{ | |
var refreshToken = false; | |
T retval = default(T); | |
do | |
{ | |
if (refreshToken) | |
{ | |
await onInvalidTokenAction(); | |
refreshToken = false; | |
} | |
try | |
{ | |
retval = await method(); | |
return retval; | |
} | |
catch (ForceException ex) | |
{ | |
if (ex.Message == "Session expired or invalid") | |
{ | |
refreshToken = true; | |
} | |
} | |
} while (numRetries-- > 0); | |
return retval; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment