예제 #1
0
        // Static constructor
        static GoogleDriveManager()
        {
            //Initialize config
            _config = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);


            // Login
            UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                new ClientSecrets
            {
                ClientId     = _config.AppSettings.Settings["client_id"].Value,
                ClientSecret = _config.AppSettings.Settings["client_secret"].Value
            },
                new[] { _config.AppSettings.Settings["scope"].Value },
                "user",
                CancellationToken.None,
                new FileDataStore(@"D:\Studying", true)).Result;

            _Token     = credential.Token.AccessToken;
            _TokenType = credential.Token.TokenType;


// Get all files from drive.
#if GOOGLEAPI_V3
            var client  = new RestClient($"https://www.googleapis.com/drive/v3/files?key={_config.AppSettings.Settings["api_key"].Value}&fields=* ");
            var request = new RestRequest(Method.GET);
            request.AddHeader("Authorization", $"{credential.Token.TokenType} {credential.Token.AccessToken}");
            IRestResponse response = client.Execute(request);
            files = NewTonSoft.FromJson(response.Content).Files;
            names = files.Select(file => file.Name).ToArray();
#else
            var client  = new RestClient($"https://www.googleapis.com/drive/v2/files?key={_config.AppSettings.Settings["api_key"].Value}");
            var request = new RestRequest(Method.GET);
            request.AddHeader("Authorization", $"{credential.Token.TokenType} {credential.Token.AccessToken}");
            IRestResponse response = client.Execute(request);
            files = GoogleDriveAPI_V2.NewTonSoftV2.FromJson(response.Content).Items;
            names = files.Select(file => file.Title).ToArray();
#endif
        }
예제 #2
0
 public static string ToJson(this NewTonSoft self) => JsonConvert.SerializeObject(self, Converter.Settings);