Exemple #1
0
        /// <summary>The core logic for asynchronously authorizing the specified user.</summary>
        /// <param name="initializer">The authorization code initializer.</param>
        /// <param name="scopes">
        /// The scopes which indicate the Google API access your application is requesting.
        /// </param>
        /// <param name="user">The user to authenticate.</param>
        /// <param name="taskCancellationToken">Cancellation token to cancel an operation.</param>
        /// <returns>User credential.</returns>
        private static async Task <UserCredential> AuthorizeAsyncCore(AuthorizationCodeFlow.Initializer initializer,
                                                                      IEnumerable <string> scopes, string user, CancellationToken taskCancellationToken)
        {
            initializer.Scopes    = scopes;
            initializer.DataStore = new StorageDataStore();

            var installedApp = new AuthorizationCodeWPInstalledApp(initializer);

            return(await installedApp.AuthorizeAsync(user, taskCancellationToken).ConfigureAwait(false));
        }
        /// <summary>The core logic for asynchronously authorizing the specified user.</summary>
        /// <param name="initializer">The authorization code initializer.</param>
        /// <param name="scopes">
        /// The scopes which indicate the Google API access your application is requesting.
        /// </param>
        /// <param name="user">The user to authorize.</param>
        /// <param name="taskCancellationToken">Cancellation token to cancel an operation.</param>
        /// <param name="dataStore">The data store, if not specified a file data store will be used.</param>
        /// <returns>User credential.</returns>
        private static async Task <UserCredential> AuthorizeAsyncCore(AuthorizationCodeFlow.Initializer initializer,
                                                                      IEnumerable <string> scopes, string user, CancellationToken taskCancellationToken,
                                                                      IDataStore dataStore = null)
        {
            initializer.Scopes    = scopes;
            initializer.DataStore = dataStore ?? new FileDataStore(Folder);
            var flow = new GoogleAuthorizationCodeFlow(initializer);

            // Create authorization code installed app instance and authorize the user.
            return(await new AuthorizationCodeInstalledApp(flow, new LocalServerCodeReceiver()).AuthorizeAsync
                       (user, taskCancellationToken).ConfigureAwait(false));
        }
Exemple #3
0
        /// <summary>
        /// Creates Google Drive service object with correct credentials.
        /// </summary>
        /// <param name="refreshToken">Refresh Token to use.</param>
        /// <returns>Google Drive service object.</returns>
        private async Task <DriveService> CreateService(string refreshToken)
        {
            var initializer =
                new AuthorizationCodeFlow.Initializer(
                    "https://accounts.google.com/o/oauth2/auth",
                    "https://accounts.google.com/o/oauth2/token")
            {
                ClientSecrets =
                    new ClientSecrets
                {
                    ClientId     = Constants.GoogleDriveSyncClientId,
                    ClientSecret = Constants.GoogleDriveSyncClientSecret
                }
            };

            // Hardcoded Google Drive Sync secret.
            string[] scopes = { DriveService.Scope.DriveReadonly };
            initializer.Scopes = scopes;

            using (var codeFlow = new AuthorizationCodeFlow(initializer))
            {
                try
                {
                    var token = await GoogleRequestHelper.Execute(
                        ct => codeFlow.RefreshTokenAsync("user", refreshToken, ct),
                        this.CancellationToken);

                    var credential = new UserCredential(codeFlow, "user", token);

                    var service =
                        new DriveService(
                            new BaseClientService.Initializer
                    {
                        HttpClientInitializer = credential,
                        ApplicationName       = Utils.Constants.ApplicationName
                    });

                    return(service);
                }
                catch (Exception e)
                {
                    if (this.CancellationToken.IsCancellationRequested)
                    {
                        this.Log.LogMessage("Operation was cancelled.");
                        throw;
                    }

                    this.StatusAccumulator.FailureOccurred();
                    this.Log.LogError("Authorization error.", e);
                    throw;
                }
            }
        }
Exemple #4
0
        /// <summary>
        /// function generates the google oauth credentials
        /// </summary>
        /// <returns></returns>
        public Google.Apis.Auth.OAuth2.UserCredential getGoogleOauthCredentials()
        {
            Google.Apis.Auth.OAuth2.Responses.TokenResponse responseData = new Google.Apis.Auth.OAuth2.Responses.TokenResponse();
            responseData.AccessToken  = this.Token;
            responseData.RefreshToken = this.RefreshToken;
            GoogleAuthorizationCodeFlow.Initializer myInit         = new GoogleAuthorizationCodeFlow.Initializer();
            AuthorizationCodeFlow.Initializer       codeFlowIntial = myInit;
            codeFlowIntial.ClientSecrets = new ClientSecrets();
            string googleClientId     = ConfigurationManager.AppSettings["googleClientId"];
            string googleClientSecret = ConfigurationManager.AppSettings["googleClientSecret"];

            codeFlowIntial.ClientSecrets.ClientId     = googleClientId;
            codeFlowIntial.ClientSecrets.ClientSecret = googleClientSecret;
            IAuthorizationCodeFlow myFlow = AppFlowMetadata.getFlow();

            Google.Apis.Auth.OAuth2.UserCredential RetValue = new Google.Apis.Auth.OAuth2.UserCredential(myFlow, this.ID, responseData);
            return(RetValue);
        }
        /// <summary>Creates an authorization code flow with the given parameters.</summary>
        /// <param name="dataStore">The data store.</param>
        /// <param name="scopes">The Scopes.</param>
        /// <param name="httpClientFactory">The HTTP client factory. If not set the default will be used.</param>
        /// <returns>Authorization code flow</returns>
        private AuthorizationCodeFlow CreateFlow(IDataStore dataStore = null, IEnumerable <string> scopes = null,
                                                 IHttpClientFactory httpClientFactory = null)
        {
            var secrets = new ClientSecrets()
            {
                ClientId = "id", ClientSecret = "secret"
            };
            var initializer = new AuthorizationCodeFlow.Initializer(AuthorizationCodeUrl, TokenUrl)
            {
                ClientSecrets     = secrets,
                HttpClientFactory = httpClientFactory
            };

            if (dataStore != null)
            {
                initializer.DataStore = dataStore;
            }
            initializer.Scopes = scopes;
            return(new AuthorizationCodeFlow(initializer));
        }
Exemple #6
0
        /// Request Oauth2 credentials through a third party by providing authorization and token server
        /// URLs.
        public OAuth2CredentialRequest(ClientSecrets secrets, IEnumerable <string> scopes,
                                       string callbackPath, IDataStore dataStore,
                                       string authorizationServerUrl, string tokenServerUrl)
        {
            Debug.Assert(!string.IsNullOrWhiteSpace(authorizationServerUrl),
                         "Missing authorization server url");
            Debug.Assert(!string.IsNullOrWhiteSpace(tokenServerUrl),
                         "Missing token server url");

            // Use the generic authorization code flow with the provided authorization and token
            // server urls.
            IsGoogle = false;
            var initializer = new AuthorizationCodeFlow.Initializer(authorizationServerUrl,
                                                                    tokenServerUrl)
            {
                ClientSecrets = secrets,
                Scopes        = scopes,
                DataStore     = dataStore,
            };
            var authFlow     = new AuthorizationCodeFlow(initializer);
            var codeReceiver = new LocalHttpCodeReceiver(callbackPath);

            m_AppFlow = new AuthorizationCodeInstalledApp(authFlow, codeReceiver);
        }
 public ForceOfflineGoogleAuthorizationCodeFlow(AuthorizationCodeFlow.Initializer initializer) : base(initializer)
 {
 }
 /// <summary>Constructs a new authorization code installed application for Windows Store.</summary>
 /// <param name="authorizationCodeFlowInitializer">A authorization code flow initializer</param>
 public AuthorizationCodeWinRTInstalledApp(AuthorizationCodeFlow.Initializer authorizationCodeFlowInitializer)
 {
     this.authorizationCodeFlowInitializer = authorizationCodeFlowInitializer;
 }
        /// <summary>
        /// Constructs a new authorization code for Windows Phone targeting an installed application flow.
        /// </summary>
        /// <param name="authorizationCodeFlowInitializer">An authorization code flow initializer.</param>
        public AuthorizationCodeWPInstalledApp(AuthorizationCodeFlow.Initializer authorizationCodeFlowInitializer)
        {
            var flow = new AuthorizationCodeFlow(authorizationCodeFlowInitializer);

            innerInstallApp = new AuthorizationCodeInstalledApp(flow, new AuthorizationCodeBroker());
        }
 /// <summary>
 /// Constructs a new authorization code for Windows Store application targeting an installed application flow.
 /// </summary>
 /// <param name="authorizationCodeFlowInitializer">An authorization code flow initializer.</param>
 public AuthorizationCodeWindowsInstalledApp(AuthorizationCodeFlow.Initializer authorizationCodeFlowInitializer)
 {
     innerInstallApp = new AuthorizationCodeInstalledApp(
         new AuthorizationCodeFlow(authorizationCodeFlowInitializer), new AuthorizationCodeBroker());
 }
 /// <summary>Constructs a new Google authorization code flow.</summary>
 public AAGoogleAuthorizationCodeFlow(AuthorizationCodeFlow.Initializer initializer)
     : base(initializer)
 {
 }