Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- internal static class Perm_AppData
- {
- private static string[] scopes = { DriveService.Scope.DriveFile };
- private static DriveService service = Autorization();
- internal static DriveService Service { get { return service; } }
- private static DriveService Autorization()
- {
- UserCredential credential;
- using (var stream = GetCliSecStream())
- {
- credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
- GoogleClientSecrets.Load(stream).Secrets,
- scopes,
- "user",
- CancellationToken.None,
- new FileDataStore(auth, true)).Result;
- }
- return new DriveService(new BaseClientService.Initializer()
- {
- HttpClientInitializer = credential,
- ApplicationName = applicationName
- });
- }
- private static Stream GetCliSecStream()
- {
- var stream = new MemoryStream();
- var writer = new StreamWriter(stream);
- writer.Write(uSec);
- writer.Flush();
- stream.Position = 0;
- return stream;
- }
- }
- internal static MemoryStream DownloadFile(string fileId = null, string fileName = null)
- {
- using (new Watcher($"Download {fileName} {fileId}"))
- {
- GetRequest request = null;
- if (fileId != null)
- {
- request = service.Files.Get(fileId);
- }
- else
- {
- if (fileName != null)
- {
- request = service.Files.Get(ViewDrive(SearchParameter.name, new string[] { fileName }).First().Id);
- }
- else
- {
- throw new Exception("You need at least one not null parameter to download");
- }
- }
- request.Fields = "id, name";
- var fileMemoryStream = new MemoryStream();
- request.Download(fileMemoryStream);
- return fileMemoryStream;
- }
- }
- WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultNetworkCredentials;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement