使用Google Drive API下载文件

时间:2014-12-08 11:13:13

标签: c# google-oauth google-drive-api google-api-dotnet-client google-authentication

我已经使用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;
              }
          }

      }
  } 

1 个答案:

答案 0 :(得分:0)

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

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

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

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

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