/// <summary> /// Refreshes the access token if (a) there is a refresh token, and (b) there is not an unexpired accesstoken /// </summary> /// <returns>True if the update happened and was successful</returns> public async Task <bool> RefreshAccessToken() { return(await Task.Run <bool>(() => { // Don't refresh if no refresh token, or if current access token is still valid if (CheckAccessToken() || String.IsNullOrEmpty(AuthState.RefreshToken)) { return false; } WebServerClient client = Client(); try { client.RefreshAuthorization(AuthState); } catch (DotNetOpenAuth.Messaging.ProtocolException ex) { GDriveError error = JsonConvert.DeserializeObject <GDriveError>(ExtractResponseString(ex.InnerException as WebException)); if (error == null) { throw; } else if (error.error.CompareCurrentCultureIgnoreCase("invalid_grant") == 0) { throw new MyFlightbookException(Branding.ReBrand(Resources.LocalizedText.GoogleDriveBadAuth), ex); } else { throw new MyFlightbookException(String.Format(CultureInfo.CurrentCulture, "Error from Google Drive: {0} {1} {2}", error.error, error.error_description, error.error_uri), ex); } } return true; })); }
private static void ThrowGDriveError(Exception ex) { if (ex == null) { return; } GDriveError error = JsonConvert.DeserializeObject <GDriveError>(ExtractResponseString(ex.InnerException as WebException)); if (error == null) { throw new MyFlightbookException("Unknown error refreshing access token", ex); } else if (error.error.CompareCurrentCultureIgnoreCase("invalid_grant") == 0) { throw new UnauthorizedAccessException(Branding.ReBrand(Resources.LocalizedText.GoogleDriveBadAuth), ex); } else { throw new MyFlightbookException(String.Format(CultureInfo.CurrentCulture, "Error from Google Drive: {0} {1} {2}", error.error, error.error_description, error.error_link), ex); } }