Exemple #1
0
        private static IAuthorizationState GetAuthentication(NativeApplicationClient client)
        {
            // You should use a more secure way of storing the key here as
            // .NET applications can be disassembled using a reflection tool.
            const string STORAGE = "google.analytics.xstat";
            const string KEY = "=UwuqAtRaqe-3daV";

            // Check if there is a cached refresh token available.
            IAuthorizationState state = AuthorizationMgr.GetCachedRefreshToken(STORAGE, KEY);
            if (state != null)
            {
                try
                {
                    client.RefreshToken(state);
                    return state; // Yes - we are done.
                }
                catch (DotNetOpenAuth.Messaging.ProtocolException ex)
                {
                    CommandLine.WriteError("Using existing refresh token failed: " + ex.Message);
                }
            }

            string[] scopes = new string[] { 
            "https://www.google.com/analytics/feeds/", 
            "https://www.googleapis.com/auth/analytics.readonly" };


            // Retrieve the authorization from the user.
            state = AuthorizationMgr.RequestNativeAuthorization(client, scopes);
            AuthorizationMgr.SetCachedRefreshToken(STORAGE, KEY, state);

            return state;
        }
        private static IAuthorizationState GetAuthorization(NativeApplicationClient client)
        {
            // You should use a more secure way of storing the key here as
            // .NET applications can be disassembled using a reflection tool.
            const string STORAGE = "latitude.client";
            const string KEY = "45dfgvq2345";

            // Check if there is a cached refresh token available.
            IAuthorizationState state = AuthorizationMgr.GetCachedRefreshToken(STORAGE, KEY);
            if (state != null)
            {
                try
                {
                    client.RefreshToken(state);
                    return state; // Yes - we are done.
                }
                catch (DotNetOpenAuth.Messaging.ProtocolException ex)
                {
                    CommandLine.WriteError("Using existing refresh token failed: " + ex.Message);
                }
            }

            // Retrieve the authorization from the user.
            state = AuthorizationMgr.RequestNativeAuthorization(client, Scope);
            AuthorizationMgr.SetCachedRefreshToken(STORAGE, KEY, state);
            return state;
        }
Exemple #3
0
        private static IAuthorizationState GetAuthentication(NativeApplicationClient client)
        {
            // You should use a more secure way of storing the key here as
            // .NET applications can be disassembled using a reflection tool.
            const string STORAGE = "google.samples.dotnet.prediction";
            const string KEY = "AF41sdBra7ufra)VD:@#A#a++=3e";
            string scope = PredictionService.Scopes.Prediction.GetStringValue();

            // Check if there is a cached refresh token available.
            IAuthorizationState state = AuthorizationMgr.GetCachedRefreshToken(STORAGE, KEY);
            if (state != null)
            {
                try
                {
                    client.RefreshToken(state);
                    return state; // Yes - we are done.
                }
                catch (DotNetOpenAuth.Messaging.ProtocolException ex)
                {
                    CommandLine.WriteError("Using existing refresh token failed: " + ex.Message);
                }
            }

            // Retrieve the authorization from the user.
            state = AuthorizationMgr.RequestNativeAuthorization(client, scope);
            AuthorizationMgr.SetCachedRefreshToken(STORAGE, KEY, state);
            return state;
        }
Exemple #4
0
        private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
        {
            // Get the auth URL:
            IAuthorizationState state = new AuthorizationState(new[] { DriveService.Scopes.Drive.GetStringValue() });
            state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
            if (!String.IsNullOrWhiteSpace(RefreshToken)) {
                state.RefreshToken = RefreshToken;

                if (arg.RefreshToken(state))
                    return state;
            }
            return state;
        }
        public static GoogleAuthenticator RefreshAuthenticator(string refreshToken)
        {
            var state = new AuthorizationState(_scopes)
            {
                RefreshToken = refreshToken
            };

            var client = new NativeApplicationClient(GoogleAuthenticationServer.Description, _clientId, _clientSecret);
            bool result = client.RefreshToken(state);

            var auth = new OAuth2Authenticator<NativeApplicationClient>(client, (c) => state);
            auth.LoadAccessToken();

            return new GoogleAuthenticator(auth);
        }
    private static IAuthorizationState GetAuthorization(NativeApplicationClient client)
    {
      var storage = MethodBase.GetCurrentMethod().DeclaringType.ToString();
      var key = "storage_key";

      IAuthorizationState state = AuthorizationMgr.GetCachedRefreshToken(storage, key);
      if (state != null)
      {
        client.RefreshToken(state);
      }
      else
      {
        state = AuthorizationMgr.RequestNativeAuthorization(client, YoutubeService.Scopes.Youtube.GetStringValue());
        AuthorizationMgr.SetCachedRefreshToken(storage, key, state);
      }

      return state;
    }
Exemple #7
0
        private static IAuthorizationState AuthProvider(NativeApplicationClient argTokenProvider)
        {
            try
            {
                IAuthorizationState state = new AuthorizationState(new[] { AnalyticsService.Scopes.Analytics.GetStringValue() });
                state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);

                string refreshToken = LoadRefreshToken();
                if (!String.IsNullOrEmpty(refreshToken))
                {
                    state.RefreshToken = refreshToken;

                    if (argTokenProvider.RefreshToken(state, null))
                        return state;
                }

                Uri authUri = argTokenProvider.RequestUserAuthorization(state);

                Process.Start(authUri.ToString());
                Console.Write(" authorization Code: ");
                string authCode = Console.ReadLine(); //4/pBCEKiIoAJCjvZSgKaEw03eO0gdk.ctpr2xLkGjoVgrKXntQAax0QqPmbcwI
                Console.WriteLine();

                // Retrieve the access token by using the authorization code:
                var result = argTokenProvider.ProcessUserAuthorization(authCode, state);

                StoreRefreshToken(result);

                return result;

            }
            catch (Exception ex)
            {
                throw;
            }
        }
Exemple #8
0
        private IAuthorizationState GetAuthentication(NativeApplicationClient client)
        {
            // You should use a more secure way of storing the key here as
            // .NET applications can be disassembled using a reflection tool.
            const string STORAGE = "google.musicplayer.dotnet.drive";
            const string KEY = "y},drdzf11x9;87";
            string scope = DriveService.Scopes.Drive.GetStringValue();

            // Check if there is a cached refresh token available.
            IAuthorizationState state = AuthorizationMgr.GetCachedRefreshToken(STORAGE, KEY);
            if (state != null)
            {
                try
                {
                    client.RefreshToken(state);
                    return state; // Yes - we are done.
                }
                catch (DotNetOpenAuth.Messaging.ProtocolException ex)
                {

                }
            }
            // Retrieve the authorization from the user.
            state = AuthorizationMgr.RequestNativeAuthorization(client, scope);
            AuthorizationMgr.SetCachedRefreshToken(STORAGE, KEY, state);
            return state;
        }
        private static IAuthorizationState GetAuthentication(NativeApplicationClient arg)
        {
            // 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 == "")
            {
                // Request authorization from the user (by opening a browser window):
                Process.Start(authUri.ToString());

                EnterAuthorizationCode eac = new EnterAuthorizationCode();
                if (eac.ShowDialog() == DialogResult.OK)
                {
                    // 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;
                    XMLManager.export(Settings.Instance, MainForm.FILENAME);

                    return result;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                arg.RefreshToken(state, null);
                result = state;
                return result;
            }
        }
        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;
        }
Exemple #11
0
        private static IAuthorizationState GetAuthentication(NativeApplicationClient client)
        {
            // You should use a more secure way of storing the key here as
            // .NET applications can be disassembled using a reflection tool.

            LoadStorageKey();

            // Check if there is a cached refresh token available.
            IAuthorizationState state = GetCachedRefreshToken(Storage, Key);
            if (state != null)
            {
                try
                {
                    client.RefreshToken(state);
                    return state; // Yes - we are done.
                }
                catch (DotNetOpenAuth.Messaging.ProtocolException ex)
                {
                    string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
                    if (!File.Exists(path.Replace("file:\\", "") + "\\tokenrefreshlog.xml"))
                    {
                        System.IO.File.AppendAllText(path.Replace("file:\\", "") + "\\tokenrefreshlog.xml", "<logs></logs>");
                    }
                    else
                    {
                        try
                        {
                            XmlDocument xmldoc = new XmlDocument();
                            xmldoc.Load(path.Replace("file:\\", "") + "\\tokenrefreshlog.xml");
                            XmlElement root = xmldoc["logs"];
                            if (root != null)
                            {
                                XmlElement log = xmldoc.CreateElement("log");
                                XmlAttribute att = xmldoc.CreateAttribute("datetime");
                                att.Value = SyncManager.ExecutingDate.ToString("dd.MM.yyyy HH:mm:ss");
                                log.InnerText = ex.Message;
                                root.AppendChild(log);
                                xmldoc.Save(path.Replace("file:\\", "") + "\\tokenrefreshlog.xml");
                            }
                        }
                        catch
                        {
                        }
                    }
                    //CommandLine.WriteError("Using existing refresh token failed: " + ex.Message);
                    state = null;
                }
            }

            return state;

            string[] scopes = new string[] { 
            "https://www.google.com/analytics/feeds/", 
            "https://www.googleapis.com/auth/analytics.readonly" };


            // Retrieve the authorization from the user.
            state = AuthorizationMgr.RequestNativeAuthorization(client, scopes);
            SetCachedRefreshToken(Storage, Key, state);
                                 
            return state;
        }
    /// <summary>
    /// Method to get the authorization from the user to access their Google Drive from the application
    /// </summary>
    /// <param name="client"></param>
    /// <returns></returns>
    private static IAuthorizationState GetAuthorization(NativeApplicationClient client)
    {
        // You should use a more secure way of storing the key here as
        // .NET applications can be disassembled using a reflection tool.
        const string KEY = "z},drdzf11x9;87";
        string scope = DriveService.Scopes.Drive.GetStringValue();

        // Check if there is a cached refresh token available.
        IAuthorizationState state = AuthorizationMgr.GetCachedRefreshToken(STORAGE, KEY);
        if (state != null)
        {
            try
            {
                client.RefreshToken(state);
                return state; // Yes - we are done.
            }
            catch (DotNetOpenAuth.Messaging.ProtocolException ex)
            {
                Debug.WriteLine("Using existing refresh token failed: " + ex.Message);
            }
        }

        // If we get here, there is no stored token. Retrieve the authorization from the user.
        state = AuthorizationMgr.RequestNativeAuthorization(client, scope);
        AuthorizationMgr.SetCachedRefreshToken(STORAGE, KEY, state);
        return state;
    }
 private static IAuthorizationState googledrive_getauthorization(NativeApplicationClient client)
 {
     string[] scopes = new string[1] { "" };
     IAuthorizationState state = new AuthorizationState(scopes) { RefreshToken = configurations.googledrive_refresh_token };
     if (state != null)
     {
         client.RefreshToken(state);
     }
     return state;
 }
        private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
        {
            IAuthorizationState state = new AuthorizationState(new[] { CalendarService.Scopes.Calendar.GetStringValue() });
            state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);

            string path = "auth.txt";
            if (File.Exists(path))
            {

                state.RefreshToken = File.ReadAllText(path);
                arg.RefreshToken(state);
                //state = arg.ProcessUserAuthorization(state.AccessToken, state);
            }
            else
            {

                Uri authUri = arg.RequestUserAuthorization(state);

                // Request authorization from the user (by opening a browser window):
                Process.Start(authUri.ToString());
                Console.Write("  Authorization Code: ");
                string authCode = string.Empty;
                int count = 0;
                while (string.IsNullOrEmpty(authCode) && count < 500)
                {
                    foreach (Process proc in Process.GetProcesses())
                    {
                        if (proc.MainWindowTitle.StartsWith("Success code="))
                        {
                            authCode = proc.MainWindowTitle.Split('=').Last().Split(' ').First();
                        }
                    }
                    Thread.Sleep(50);
                    Application.DoEvents();
                    count++;
                }
                Console.WriteLine();
                // Retrieve the access token by using the authorization code:
                state = arg.ProcessUserAuthorization(authCode, state);
            }

            File.WriteAllText(path, state.RefreshToken);

            return state;
        }
Exemple #15
0
        private IAuthorizationState GetAuthorization(NativeApplicationClient arg)
        {
            // Get the auth URL:
            IAuthorizationState state = new AuthorizationState(new[] { DriveService.Scopes.Drive.GetStringValue() });
            state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
            IAuthorizationState result = null;
            string refreshToken = LoadRefreshToken();
            if (!String.IsNullOrEmpty(refreshToken))
            {
                state.RefreshToken = refreshToken;

                if (arg.RefreshToken(state))
                    return state;
            }
            frmAuthorizationCode ac = new frmAuthorizationCode();
            Uri authUri = arg.RequestUserAuthorization(state);

            // Request authorization from the user (by opening a browser window):
            Process.Start(authUri.ToString());

            if (ac.ShowDialog() == DialogResult.OK)
            {
                result = arg.ProcessUserAuthorization(ac.txtAC.Text, state);
                StoreRefreshToken(state);
            }
            return result;
        }
        private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
        {
            // Get the auth URL:
            IAuthorizationState state =
                new AuthorizationState(
                    new[]
                        {
                            Google.Apis.Admin.Directory.directory_v1.DirectoryService.Scopes.AdminDirectoryUser.GetStringValue(),
                            Google.Apis.Admin.Directory.directory_v1.DirectoryService.Scopes.AdminDirectoryUserAlias.GetStringValue(),
                            Google.Apis.Admin.Directory.directory_v1.DirectoryService.Scopes.AdminDirectoryOrgunit.GetStringValue(),
                            Google.Apis.Admin.Directory.directory_v1.DirectoryService.Scopes.AdminDirectoryGroupMember.GetStringValue(),
                            Google.Apis.Admin.Directory.directory_v1.DirectoryService.Scopes.AdminDirectoryDeviceChromeos.GetStringValue(),
                            Google.Apis.Admin.Directory.directory_v1.DirectoryService.Scopes.AdminDirectoryDeviceMobile.GetStringValue(),
                            Google.Apis.Admin.Directory.directory_v1.DirectoryService.Scopes.AdminDirectoryDeviceMobileAction.GetStringValue(),
                            GroupssettingsService.Scopes.AppsGroupsSettings.GetStringValue() });

            state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);

            if (!String.IsNullOrWhiteSpace(_refreshToken))
            {
                state.RefreshToken = _refreshToken;

                if (arg.RefreshToken(state))
                    return state;
            }

            var authUri = arg.RequestUserAuthorization(state);

            // Request authorization from the user (by opening a browser window):
            Process.Start(authUri.ToString());
            Console.Write("  Authorization Code: ");
            var authCode = Console.ReadLine();
            Console.WriteLine();

            // Retrieve the access token by using the authorization code:
            var result = arg.ProcessUserAuthorization(authCode, state);
            Console.WriteLine("Refresh Token: " + result.RefreshToken);

            return result;
        }
Exemple #17
0
        private IAuthorizationState GetAuthorization(NativeApplicationClient arg)
        {
            // Check if there is a cached refresh token available.
            String[] scopes = new String[]
            {
                "https://www.googleapis.com/auth/drive",
                "https://docs.google.com/feeds",
                "https://www.googleapis.com/auth/userinfo.email",
                "https://www.googleapis.com/auth/userinfo.profile"
            };
            //const string STORAGE = "eCubeDMS";
            //const string KEY = "4/BgrfOgMaqaH6duTF-ZK7yBeoxiYn.8tlx19KH7tAeXE-sT2ZLcbQJoY-TdAI";

            //IAuthorizationState state = AuthorizationMgr.GetCachedRefreshToken(STORAGE, KEY);
            IAuthorizationState state = AuthorizationMgr.GetCachedRefreshToken(ConnectionString);
            if (state != null)
            {
                try
                {
                    arg.RefreshToken(state);
                    return state;
                    // Yes - we are done.
                }
                catch (DotNetOpenAuth.Messaging.ProtocolException ex)
                {
                    throw new Exception("Using existing refresh token failed: " + ex.Message);
                }
            }

            // Retrieve the authorization from the user.
            state = AuthorizationMgr.RequestNativeAuthorization(arg, scopes);
            //AuthorizationMgr.SetCachedRefreshToken(STORAGE, KEY, state);
            AuthorizationMgr.SetCachedRefreshToken(state, ConnectionString);
            return state;
        }
Exemple #18
0
 private IAuthorizationState GetAuthorization(NativeApplicationClient appClient)
 {
     var state = new AuthorizationState(new[] {"https://www.googleapis.com/auth/drive"}) {Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl), RefreshToken = mRefreshToken.Token};
       appClient.RefreshToken(state);
       return state;
 }
        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;
            }

        }