Google drive service account download issue

2019-04-14 07:11发布

问题:

I created service account, I exposed several files to this account and once I wanted to download files it throws following exception

Google.Apis.Requests.RequestError Bad Request [400] Errors [ Message[Bad Request] Location[ - ] Reason[badRequest] Domain[global] ]

 var certificate = new X509Certificate2("*******.p12", "notasecret", X509KeyStorageFlags.Exportable);
        try
        {
            ServiceAccountCredential credential = new ServiceAccountCredential(
                new ServiceAccountCredential.Initializer("*******..com")
               {
                   Scopes = scopes
               }.FromCertificate(certificate));

            // Create the service.
            DriveService service = new DriveService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "Daimto Drive API Sample",
            });
            var files = service.Files.List().Execute().Files;
            var request = service.Files.Get(files.First().Id);

            var stream = new System.IO.MemoryStream(); 
            request.MediaDownloader.ProgressChanged +=
                (IDownloadProgress progress) =>
                {
                    switch (progress.Status)
                    {
                        case DownloadStatus.Downloading:
                            {
                                Console.WriteLine(progress.BytesDownloaded);
                                break;
                            }
                        case DownloadStatus.Completed:
                            {
                                Console.WriteLine("Download complete.");
                                break;
                            }
                        case DownloadStatus.Failed:
                            {
                                Console.WriteLine("Download failed.");
                                break;
                            }
                    }
                };
            request.Download(stream);