예제 #1
0
            public Google.Apis.Drive.v2.DriveService Init(string applicationName,
                                                    string clientIdentifier,
                                                    string clientSecret,
                                                    string[] scope,
                                                    bool logout,
                                                    string refreshTokenFolder)
            {
                StatusForm.ShowDialog();

                try
                {
                  _refreshTokenFilePath = refreshTokenFolder;

                  string fileDataStorePath = _refreshTokenFilePath;
                  if (FileExists(fileDataStorePath))
                  {
                DateTime lastFileWriteTime = GetFileLastWriteTime(fileDataStorePath);

                if (lastFileWriteTime < new DateTime(2014, 8, 22))
                {
                  DeleteFile(fileDataStorePath);
                }
                  }

                  if (logout)
                  {
                try
                {
                  if (DirectoryExists(fileDataStorePath))
                  {
                string[] fileDataStoreFiles = System.IO.Directory.GetFiles(fileDataStorePath,
                                                                           "Google.Apis.Auth.OAuth2.Responses.*");

                foreach (string fileDataStoreFile in fileDataStoreFiles)
                {
                  DeleteFile(fileDataStoreFile);
                }
                  }

                  LoginForm.Logout();
                }
                catch (Exception exception)
                {
                  Log.Error(String.Format("Authenticator.Logout - Deleting token file. {0}",exception.Message));
                }
                  }

                  var clientSecrets = new Google.Apis.Auth.OAuth2.ClientSecrets
                  {
                ClientId = clientIdentifier,
                ClientSecret = clientSecret
                  };

                  _fileDataStore = new EncryptedFileDataStore(refreshTokenFolder);

                  System.Threading.Tasks.Task<Google.Apis.Auth.OAuth2.UserCredential> task = null;

                  try
                  {
                task = Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.AuthorizeAsync(
                                                                                       clientSecrets,
                                                                                       scope,
                                                                                       System.Environment.UserName,
                                                                                       System.Threading.CancellationToken.None,
                                                                                       _fileDataStore);

                task.Wait();
                  }
                  catch (Exception exception)
                  {
                throw new LogException("GoogleWebAuthorizationBroker.AuthorizeAsync - " + exception.Message, false, false);
                  }

                  _userCredential = task.Result;

                  var initializer = new BaseClientService.Initializer
                  {
                HttpClientInitializer = _userCredential,
                ApplicationName = applicationName
                  };

                  _driveService = new Google.Apis.Drive.v2.DriveService(initializer);

                  UpdateAboutData(_driveService);

                  return _driveService;
                }
                catch (Exception exception)
                {
                  Log.Error(exception);

                  return null;
                }
                finally
                {
                  StatusForm.CloseDialog();
                }
            }
예제 #2
0
            public Google.Apis.Drive.v2.DriveService Init(string applicationName,
                                                          string clientIdentifier,
                                                          string clientSecret,
                                                          string[] scope,
                                                          bool logout,
                                                          string refreshTokenFolder)
            {
                StatusForm.ShowDialog();

                try
                {
                    _refreshTokenFilePath = refreshTokenFolder;

                    string fileDataStorePath = _refreshTokenFilePath;
                    if (FileExists(fileDataStorePath))
                    {
                        DateTime lastFileWriteTime = GetFileLastWriteTime(fileDataStorePath);

                        if (lastFileWriteTime < new DateTime(2014, 8, 22))
                        {
                            DeleteFile(fileDataStorePath);
                        }
                    }

                    if (logout)
                    {
                        try
                        {
                            if (DirectoryExists(fileDataStorePath))
                            {
                                string[] fileDataStoreFiles = System.IO.Directory.GetFiles(fileDataStorePath,
                                                                                           "Google.Apis.Auth.OAuth2.Responses.*");

                                foreach (string fileDataStoreFile in fileDataStoreFiles)
                                {
                                    DeleteFile(fileDataStoreFile);
                                }
                            }

                            LoginForm.Logout();
                        }
                        catch (Exception exception)
                        {
                            Log.Error(String.Format("Authenticator.Logout - Deleting token file. {0}", exception.Message));
                        }
                    }

                    var clientSecrets = new Google.Apis.Auth.OAuth2.ClientSecrets
                    {
                        ClientId     = clientIdentifier,
                        ClientSecret = clientSecret
                    };

                    _fileDataStore = new EncryptedFileDataStore(refreshTokenFolder);

                    System.Threading.Tasks.Task <Google.Apis.Auth.OAuth2.UserCredential> task = null;

                    try
                    {
                        task = Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.AuthorizeAsync(
                            clientSecrets,
                            scope,
                            System.Environment.UserName,
                            System.Threading.CancellationToken.None,
                            _fileDataStore);

                        task.Wait();
                    }
                    catch (Exception exception)
                    {
                        throw new LogException("GoogleWebAuthorizationBroker.AuthorizeAsync - " + exception.Message, false, false);
                    }

                    _userCredential = task.Result;

                    var initializer = new BaseClientService.Initializer
                    {
                        HttpClientInitializer = _userCredential,
                        ApplicationName       = applicationName
                    };

                    _driveService = new Google.Apis.Drive.v2.DriveService(initializer);

                    UpdateAboutData(_driveService);

                    return(_driveService);
                }
                catch (Exception exception)
                {
                    Log.Error(exception);

                    return(null);
                }
                finally
                {
                    StatusForm.CloseDialog();
                }
            }