private IAuthorizationState getAuthentication(NativeApplicationClient arg) { log.Debug("Authenticating with Google calendar service..."); // Get the auth URLs: IAuthorizationState state = new AuthorizationState(new[] { CalendarService.Scopes.Calendar.GetStringValue(), "https://www.googleapis.com/auth/userinfo.email" }); state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl); state.RefreshToken = Settings.Instance.RefreshToken; Uri authUri = arg.RequestUserAuthorization(state); IAuthorizationState result = null; if (state.RefreshToken == "") { log.Info("No refresh token available - need user authorisation."); // Request authorization from the user (by opening a browser window): Process.Start(authUri.ToString()); frmGoogleAuthorizationCode eac = new frmGoogleAuthorizationCode(); if (eac.ShowDialog() == DialogResult.OK) { if (string.IsNullOrEmpty(eac.authcode)) log.Debug("User continued but did not provide a code! This isn't going to work..."); else log.Debug("User has provided authentication code."); // Retrieve the access/refresh tokens by using the authorization code: result = arg.ProcessUserAuthorization(eac.authcode, state); //save the refresh token for future use Settings.Instance.RefreshToken = result.RefreshToken; Settings.Instance.Save(); log.Info("Refresh and Access token successfully retrieved."); getGaccountEmail(result.AccessToken, true); } else { log.Info("User declined to provide authorisation code. Sync will not be able to work."); String noAuth = "Sorry, but this application will not work if you don't give it access to your Google Calendar :("; MessageBox.Show(noAuth, "Authorisation not given", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); throw new System.ApplicationException(noAuth); } } else { try { arg.RefreshToken(state, null); if (string.IsNullOrEmpty(state.AccessToken)) log.Error("Failed to retrieve Access token."); else log.Debug("Access token refreshed - expires " + ((DateTime)state.AccessTokenExpirationUtc).ToLocalTime().ToString()); } catch (System.Exception ex) { log.Error(ex.GetType().ToString() + " " + ex.Message); log.Error(ex.StackTrace); MainForm.Instance.Logboxout("Failed to obtain Calendar access from Google - it's possible your access has been revoked." + "\r\nTry disconnecting your Google account and reauthenticating."); } result = state; getGaccountEmail(result.AccessToken, false); } return result; }
private static IAuthorizationState getAuthentication(NativeApplicationClient arg) { log.Debug("Authenticating with Google calendar service..."); // Get the auth URL: IAuthorizationState state = new AuthorizationState(new[] { CalendarService.Scopes.Calendar.GetStringValue() }); state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl); state.RefreshToken = Settings.Instance.RefreshToken; Uri authUri = arg.RequestUserAuthorization(state); IAuthorizationState result = null; if (state.RefreshToken == "") { log.Info("No refresh token available - need user authorisation."); // Request authorization from the user (by opening a browser window): Process.Start(authUri.ToString()); frmGoogleAuthorizationCode eac = new frmGoogleAuthorizationCode(); if (eac.ShowDialog() == DialogResult.OK) { if (string.IsNullOrEmpty(eac.authcode)) log.Debug("User continued but did not provide a code! This isn't going to work..."); else log.Debug("User has provided authentication code."); // Retrieve the access/refresh tokens by using the authorization code: result = arg.ProcessUserAuthorization(eac.authcode, state); //save the refresh token for future use Settings.Instance.RefreshToken = result.RefreshToken; Settings.Instance.Save(); log.Info("Refresh and Access token successfully retrieved."); return result; } else { log.Info("User declined to provide authorisation code. Sync will not be able to work."); String noAuth = "Sorry, but this application will not work if you don't give it access to your Google Calendar :("; MessageBox.Show(noAuth, "Authorisation not given", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); throw new System.ApplicationException(noAuth); } } else { arg.RefreshToken(state, null); if (string.IsNullOrEmpty(state.AccessToken)) log.Error("Failed to retrieve Access token."); else log.Debug("Access token refreshed - expires " + ((DateTime)state.AccessTokenExpirationUtc).ToLocalTime().ToString()); result = state; return result; } }