Ejemplo n.º 1
0
        /// <summary>
        /// Check credentials to access Google Drive Cloud Storage.
        /// </summary>
        /// <param name="credentials">requied parameters for Google Drive OAuth 2.0 authentication.</param>
        public static bool CheckCredentials(GoogleDriveCredentials credentials)
        {
            var flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
            {
                ClientSecrets = new ClientSecrets
                {
                    ClientId     = credentials.ClientId,
                    ClientSecret = credentials.ClientSecret
                },
                Scopes = Scopes
            });

            var accessToken = Reporting.DataProviders.GoogleDataProviderHelper.GetAuthToken(credentials.RefreshToken, credentials.ClientId, credentials.ClientSecret);

            var token = new TokenResponse
            {
                AccessToken  = accessToken,
                RefreshToken = credentials.RefreshToken
            };

            var credential = new UserCredential(flow, Environment.UserName, token);

            var service = new DriveService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName       = credentials.ApplicationName
            });
            var aboutRequest = service.About.Get();

            aboutRequest.Fields = "user";
            var about = aboutRequest.Execute();

            return(about.User.DisplayName != string.Empty);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Uploads given content to a file in the Google Drive Cloud Storage.
        /// </summary>
        /// <param name="ll">current instance of List & Label</param>
        /// <param name="uploadParameters">requied parameters for Google Drive OAuth 2.0 upload silently.</param>
        /// <param name="credentials">requied parameters for Google Drive OAuth 2.0 authentication.</param>
        public static void UploadSilently(this Reporting.ListLabel ll, GoogleDriveUploadParameter uploadParameters, GoogleDriveCredentials credentials)
        {
            var flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
            {
                ClientSecrets = new ClientSecrets
                {
                    ClientId     = credentials.ClientId,
                    ClientSecret = credentials.ClientSecret
                },
                Scopes = Scopes
            });

            var accessToken = Reporting.DataProviders.GoogleDataProviderHelper.GetAuthToken(credentials.RefreshToken, credentials.ClientId, credentials.ClientSecret);

            var token = new TokenResponse
            {
                AccessToken  = accessToken,
                RefreshToken = credentials.RefreshToken
            };

            var credential = new UserCredential(flow, Environment.UserName, token);

            Upload2GoogleDrive(credential, uploadParameters.UploadStream, uploadParameters.CloudFileName, uploadParameters.CloudPath, uploadParameters.ApplicationName, uploadParameters.MimeType);
        }