Flutter:Google云端硬盘:文件列表始终返回0 [英] Flutter: Google Drive: File list always returns me 0

查看:102
本文介绍了Flutter:Google云端硬盘:文件列表始终返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Google驱动器文件夹中检索文件列表。身份验证通过服务帐户进行。这是我的代码,该操作可以执行以下操作:

I want to retrieve list of files from a Google drive folder. Authentication happens through Service account. Here is my code to do the same:

final _credentials = new ServiceAccountCredentials.fromJson(r'''
{
  "private_key_id": "b5-xxxx-17",
  "private_key": "-----BEGIN PRIVATE KEY-----\nMI-xxxxk=\n-----END PRIVATE KEY-----\n",
  "client_email": "drive-access@xxxx.iam.gserviceaccount.com",
  "client_id": "100000000000",
  "type": "service_account"
}
''');

final _SCOPES = [SheetsApi.DriveFileScope, SheetsApi.SpreadsheetsScope];

clientViaServiceAccount(_credentials, _SCOPES).then((http_client) {
  DriveApi driveApi = DriveApi(http_client);
  driveApi.files.list().then((files) {
    print('kind: ' + files.kind);
    print('list: ' + files.files.length.toString());
  });

我的日志如下:

just: drive#fileList
list: 0

在Google Developers控制台中,Google Drive API已启用,并且服务帐户已正确链接(据我所知)。

In Google Developers console, Google Drive API is enabled and service account it linked properly (as far as I can check).

但是我还得到了另一段代码,该数据将一些数据写入到电子表格,使用硬编码的 sheetID 并且该代码可以正常工作。

But I also got another piece of code which writes some data to a spreadsheet, with hardcoded sheetID and that code is working fine.

对我在这里做错的任何帮助吗?

Any help on what I am doing wrong here?

推荐答案

使用服务帐户检索文件:



服务帐户是与您的Google帐户不同。这意味着S之间的Google云端硬盘也有所不同ervice帐户和您的帐户。因此,使用服务帐户检索文件时,请与服务帐户共享Google云端硬盘中的文件。这样,您的Google云端硬盘中的文件可以通过服务帐户进行检索。

Retrieving files using Service account:

The service account is different from your Google account. This means that the Google Drive is also different between Service account and your account. So when the file is retrieved using Service account, please share the files in your Google Drive with the Service account. By this, the files in your Google Drive can be retrieved by the Service account.

在您的脚本 DriveFileScope SpreadsheetsScope 用作范围。 DriveFileScope https://www.googleapis.com/auth/drive.file 官方文件对此范围的说明如下。

In your script, DriveFileScope and SpreadsheetsScope are used as the scopes. DriveFileScope is https://www.googleapis.com/auth/drive.file. The official document says this scope as follows.


查看和管理您使用此应用程序打开或创建的Google云端硬盘文件和文件夹

View and manage Google Drive files and folders that you have opened or created with this app

通过此操作,如何在脚本中按如下所示修改 DriveFileScope

By this, in your script, how about modifying DriveFileScope as follows?


  • DriveReadonlyScope https://www.googleapis.com/auth/drive.readonly

  • DriveScope https://www.googleapis.com/auth/drive

  • DriveReadonlyScope (https://www.googleapis.com/auth/drive.readonly)
  • DriveScope (https://www.googleapis.com/auth/drive)

  • 我认为该范围也可以用于您的情况。


    • DriveApi.driveMetadataReadOnlyScope https://www.googleapis.com /auth/drive.metadata.readonly

    • I think that this scope can be also used for your situation.
      • DriveApi.driveMetadataReadOnlyScope (https://www.googleapis.com/auth/drive.metadata.readonly)
      • Scopes for Google Sheets API, v4
      • Scopes for Drive API, v3

      这篇关于Flutter:Google云端硬盘:文件列表始终返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆