获得' UnauthorizedAccessException'尝试使用c#代码从Google云端硬盘下载文件时

时间:2017-07-28 07:26:46

标签: c# google-drive-api

我正在尝试使用以下代码从Google云端硬盘下载文件。 当它到达SaveStream()函数时,它会抛出一个UnauthorizedAccessException。

根据一些建议,我将该文件夹的访问权限设置为完全控制。但是它现在对我不起作用,我也得到了同样的例外。

namespace BackEnd.Controllers
{   
    public class GoogleDriveController : Controller
    {
        // GET: GoogleDrive

        static string[] Scopes = { DriveService.Scope.DriveReadonly };
        static string ApplicationName = "Drive API .NET Quickstart";
        public ActionResult Index()
        {
            UserCredential credential;
            using (var stream = new FileStream(@"D:\client_secret.json", FileMode.Open, FileAccess.Read))
            {
                        string credPath = System.Environment.GetFolderPath(
                        System.Environment.SpecialFolder.Personal);
                    credPath = Path.Combine(credPath, ".credentials/drive-dotnet-quickstart.json");

                    credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                        GoogleClientSecrets.Load(stream).Secrets,
                        Scopes,
                        "user",
                        CancellationToken.None,
                        new FileDataStore(credPath, true)).Result;
                  //  Console.WriteLine("Credential file saved to: " + credPath);
                }

                // Create Drive API service.
                var service = new DriveService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = ApplicationName,
                });

                // Define parameters of request.
                FilesResource.ListRequest listRequest = service.Files.List();
                listRequest.MaxResults = 10;    

                IList<Google.Apis.Drive.v2.Data.File> files = listRequest.Execute().Items;

                if (files != null && files.Count > 0)
                {
                    foreach (var file in files)
                    {                     
                    DownloadFile(service, file, string.Format(@"D:\Photos"));
                    }
                }                   

            return View();
        }
        private static void DownloadFile(Google.Apis.Drive.v2.DriveService service, Google.Apis.Drive.v2.Data.File file, string saveTo)
        {    
            var request = service.Files.Get(file.Id);
            var stream = new System.IO.MemoryStream();

            request.MediaDownloader.ProgressChanged += (Google.Apis.Download.IDownloadProgress progress) =>
            {
                switch (progress.Status)
                {
                    case Google.Apis.Download.DownloadStatus.Downloading:
                        {
                          // (progress.BytesDownloaded);
                            break;
                        }
                    case Google.Apis.Download.DownloadStatus.Completed:
                        {
                           //"Download complete.";
                            SaveStream(stream, saveTo);
                            break;
                        }
                    case Google.Apis.Download.DownloadStatus.Failed:
                        {
                           //"Download failed.";
                            break;
                        }
                }
            };
            request.Download(stream);

        }
        private static void SaveStream(System.IO.MemoryStream stream, string saveTo)
        {
            using (System.IO.FileStream file = new System.IO.FileStream(saveTo, System.IO.FileMode.Create, System.IO.FileAccess.Write))
            {
                stream.WriteTo(file);
            }
        }
    }
}

0 个答案:

没有答案