【问题标题】:Retrieve list of files google drive using .net sdk使用.net sdk检索文件谷歌驱动器列表
【发布时间】:2013-04-30 11:35:38
【问题描述】:

我正在尝试从 Google 驱动器中获取文件列表,我正在使用他们提供的示例:

public static List<File> RetrieveAllFiles(DriveService service)
    {
        List<File> result = new List<File>();
        FilesResource.ListRequest request = service.Files.List();

        do
        {
            try
            {
                FileList files = request.Fetch();

                result.AddRange(files.Items);
                request.PageToken = files.NextPageToken;
            }
            catch (Exception e)
            {
                Console.WriteLine("An error occurred: " + e.Message);
                request.PageToken = null;
            }
        } while (!String.IsNullOrEmpty(request.PageToken));
        return result;
    }

它总是返回文件count = 0,而我很确定登录的帐户有很多文件!还有什么需要的吗?

编辑: 对于身份验证:

  public static IAuthenticator GetCredentials(String authorizationCode, String state)
    {
        String emailAddress = "";
        try
        {
            IAuthorizationState credentials = ExchangeCode(authorizationCode);
            Userinfo userInfo = GetUserInfo(credentials);
            String userId = userInfo.Id;
            emailAddress = userInfo.Email;
            if (!String.IsNullOrEmpty(credentials.RefreshToken))
            {
                StoreCredentials(userId, credentials);
                return GetAuthenticatorFromState(credentials);
            }
            else
            {
                credentials = GetStoredCredentials(userId);
                if (credentials != null && !String.IsNullOrEmpty(credentials.RefreshToken))
                {
                    return GetAuthenticatorFromState(credentials);
                }
            }
        }
        catch (CodeExchangeException e)
        {
            Console.WriteLine("An error occurred during code exchange.");
            // Drive apps should try to retrieve the user and credentials for the current
            // session.
            // If none is available, redirect the user to the authorization URL.
            e.AuthorizationUrl = GetAuthorizationUrl(emailAddress, state);
            throw e;
        }
        catch (NoUserIdException)
        {
            Console.WriteLine("No user ID could be retrieved.");
        }
        // No refresh token has been retrieved.
        String authorizationUrl = GetAuthorizationUrl(emailAddress, state);
        throw new NoRefreshTokenException(authorizationUrl);
    }

     internal static Google.Apis.Drive.v2.DriveService BuildService(IAuthenticator credentials)
    {
        return new Google.Apis.Drive.v2.DriveService(credentials);
    }

在控制器中

  public ActionResult Index(string state, string code)
    {
        try
        {
            List<File> files = new List<File>();
            IAuthenticator authenticator = Utils.GetCredentials(code, state);
            // Store the authenticator and the authorized service in session
            Session["authenticator"] = authenticator;
            DriveService service = Utils.BuildService(authenticator);

            if (authenticator != null && service != null)
            {
                files = GoogleDriveHelper.RetrieveAllFiles(service);
                return View(files);
            }
        }
        catch (CodeExchangeException)
        {
            if (Session["service"] == null || Session["authenticator"] == null)
            {
                Response.Redirect(Utils.GetAuthorizationUrl("", state));
            }
        }
        catch (NoRefreshTokenException e)
        {
            Response.Redirect(e.AuthorizationUrl);
        }
     return View();
   }

【问题讨论】:

  • 您收到任何异常?如何授权和认证?
  • 我从 Google sdk 下载了 DrEdit,并使用了相同的类
  • 我刚刚更新了我的代码

标签: c# .net asp.net-mvc-3 google-drive-api


【解决方案1】:

您必须使用文件夹 id 来获取文件。我用它来获取文件夹文件:

string folderid = FindFolder(service, rootFolder,CreatedFolder.Title);
List<ChildReference> listadoFiles = service.Children.List(folderid).Fetch().Items.ToList();

public static string FindFolder(DriveService service,String parentfolderId, string FolderName)
{
    ChildrenResource.ListRequest request = service.Children.List(parentfolderId);
    request.Q = "mimeType='application/vnd.google-apps.folder' and title='" + FolderName + "' ";
    do
    {
        try
        {
            ChildList children = request.Fetch();

            if (children != null && children.Items.Count > 0)
            {

                return children.Items[0].Id;
            }

            foreach (ChildReference child in children.Items)
            {
                Console.WriteLine("File Id: " + child.Id);
            }
            request.PageToken = children.NextPageToken;
        }
        catch (Exception e)
        {
            Console.WriteLine("An error occurred: " + e.Message);
            request.PageToken = null;
        }
    } while (!String.IsNullOrEmpty(request.PageToken));

    return string.Empty;
}

【讨论】:

  • 在这种情况下我应该知道这个驱动器中的所有文件夹?
  • 可以获取根文件夹id,查询得到所有子文件夹,但是根文件夹有自己的id。
【解决方案2】:

我遇到了同样的问题,并通过添加缺少的驱动器范围解决了它。

【讨论】:

  • 你能用例子澄清一下吗?
  • 我添加了以下范围:https://www.googleapis.com/auth/drivehttps://www.googleapis.com/auth/drive.appdatahttps://www.googleapis.com/auth/drive.filehttps://www.googleapis.com/auth/drive.metadata.readonlyhttps://www.googleapis.com/auth/drive.readonly。 Selamu alejkum 兄弟
【解决方案3】:

为了能够列出您需要添加范围的文件和文件夹:

https://www.googleapis.com/auth/drive.appfolder”(允许只读访问文件元数据,但不允许任何访问读取或下载文件内容)

https://developers.google.com/drive/web/scopes

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多