【问题标题】:Unable to list files with GoogleDrive API无法使用 Google Drive API 列出文件
【发布时间】:2012-11-23 03:09:03
【问题描述】:

我正在尝试通过其 SDK 从 google drive 获取文档列表。我使用服务帐户完成身份验证,并将范围设置为具有完全权限的https://www.googleapis.com/auth/drive

但是,尽管我的谷歌驱动器中确实有测试文件,但响应包含一个空项目列表。

        AuthorizationServerDescription desc = GoogleAuthenticationServer.Description;
        X509Certificate2 key = new X509Certificate2(m_file, "notasecret", X509KeyStorageFlags.Exportable);

        string scope = Google.Apis.Drive.v2.DriveService.Scopes.Drive.ToString().ToLower();
        AssertionFlowClient client = new AssertionFlowClient(desc, key) { ServiceAccountId = m_email, Scope = "https://www.googleapis.com/auth/" + scope };

        OAuth2Authenticator<AssertionFlowClient> auth = new OAuth2Authenticator<AssertionFlowClient>(client, AssertionFlowClient.GetState);


        DriveService service = new DriveService(auth);

        FilesResource.ListRequest request = service.Files.List();
        request.Q = "";
        Stream dataStream = request.FetchAsStream();
        StreamReader reader = new StreamReader(dataStream);
        string responseFromServer = reader.ReadToEnd();

【问题讨论】:

    标签: google-drive-api


    【解决方案1】:

    查看 Nivco 对 Google Drive SDK: Empty array of files returned from Drive 的回答,了解有关您的问题的更多详细信息。

    您基本上是在服务帐户的云端硬盘中列出文件,而不是您的。您需要做的是为您尝试模拟的域的用户设置电子邮件,查看 Google Drive SDK 文档以获取说明和示例代码,展示如何执行此操作:

    https://developers.google.com/drive/delegation

    【讨论】:

    • 谢谢克劳迪奥。我是这个 Google Drive SDK 的新手。看来我必须是特定域的管理员才能设置我无法设置的委派。我想要做的是设置一个服务来将我的文档从 Google Drive 下载到本地数据库,应该没有 GUI 来获得身份验证。有什么建议吗?
    • 您需要通过 OAuth 流程至少一次来批准应用程序,然后您可以永久存储刷新令牌并使用它来生成新的访问令牌而无需用户交互:developers.google.com/drive/about-auth
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    • 2021-07-15
    • 2012-06-11
    • 2022-01-03
    • 1970-01-01
    相关资源
    最近更新 更多