使用谷歌云端硬盘 API 下载文件

本文关键字:API 下载 文件 硬盘 云端 谷歌 | 更新日期: 2023-09-27 18:33:26

>我已经使用Google API从Google驱动器下载文件,但不幸的是,我遇到了一个问题,即访问路径"Daimto.GoogleDrive.Auth.Store"被拒绝。我不知道如何解决这个问题,也不知道我在GoogleWebAuthorizationBroker.AuthorizeAsync()方法中使用了正确的参数。

  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using Google.Apis.Drive.v2;
  using Google.Apis.Auth.OAuth2;
  using System.Threading;
  using Google.Apis.Util.Store;
  using Google.Apis.Services;
  using Google.Apis.Drive.v2.Data;
  using System.Collections.Generic;
  namespace DownloadFromGoogleDrive
  {
      public partial class _Default : Page
      {
          protected void Page_Load(object sender, EventArgs e)
          {
              //Scopes for use with the Google Drive API
              string[] scopes = new string[] { DriveService.Scope.Drive,
                                   DriveService.Scope.DriveFile};
              // here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
              UserCredential credential =
                          GoogleWebAuthorizationBroker
                                        .AuthorizeAsync(new ClientSecrets
                                        {
                                            ClientId = "581284671387-uqf7v4ci6olktjthppd590uv8vnaqvc0.apps.googleusercontent.com"
                                        ,
                                            ClientSecret = "eZZd5zTThgKroNClKDKWR-mJ"
                                        }
                                                        , scopes
                                                        , Environment.UserName
                                                        , CancellationToken.None
                                                        , new FileDataStore("Daimto.GoogleDrive.Auth.Store")
                                                        ).Result;
              DriveService service = new DriveService(new BaseClientService.Initializer()
              {
                  HttpClientInitializer = credential,
                  ApplicationName = "Drive API Sample",
              });

          }
          /// <summary>
          /// Download a file
          /// Documentation: https://developers.google.com/drive/v2/reference/files/get
          /// </summary>
          /// <param name="_service">a Valid authenticated DriveService</param>
          /// <param name="_fileResource">File resource of the file to download</param>
          /// <param name="_saveTo">location of where to save the file including the file name to save it as.</param>
          /// <returns></returns>
          public static Boolean downloadFile(DriveService _service, File _fileResource, string _saveTo)
          {
              if (!String.IsNullOrEmpty(_fileResource.DownloadUrl))
              {
                  try
                  {
                      var x = _service.HttpClient.GetByteArrayAsync(_fileResource.DownloadUrl);
                      byte[] arrBytes = x.Result;
                      System.IO.File.WriteAllBytes(_saveTo, arrBytes);
                      return true;
                  }
                  catch (Exception e)
                  {
                      Console.WriteLine("An error occurred: " + e.Message);
                      return false;
                  }
              }
              else
              {
                  // The file doesn't have any content stored on Drive.
                  return false;
              }
          }
      }
  } 

使用谷歌云端硬盘 API 下载文件

FileDataStore 将您的身份验证信息存储在 %AppData% 目录中。

所以你最终得到的是这样的东西

C:'Users'linda_l'AppData'Roaming'Daimto.GoogleDrive.Auth.Store

您需要确保运行应用程序的进程有权访问 %AppData% 目录,或者创建您自己的 Idatastore 实现,以将文件存储在当前目录 LocalFileDataStore 的其他位置.cs

您可以在此处找到更详细地解释这一点的教程。Google Oauth2 C#