【问题标题】:Dart / flutter: download or read the contents of a Google Drive fileDart / flutter:下载或读取 Google Drive 文件的内容
【发布时间】:2018-09-13 13:40:54
【问题描述】:

我的 Google 云端硬盘上有一个公开(知道链接的任何人都可以查看)文件,我想在我的 Android 应用中使用它的内容。

从我目前收集到的信息来看,我需要文件 ID、OAuth 令牌和客户端 ID——这些我已经得到了。但我不知道授权应用程序或获取文件的确切方法是什么。

编辑:

仅使用 file.readAsLines 读取它不起作用:

final file = new File(dogListTxt);
  Future<List<String>> dogLinks = file.readAsLines();

  return dogLinks;

dogLinks 变量没有填充任何数据,但我没有收到任何错误消息。

我尝试的另一种方法是关注this example,但这是一个基于 Web 的应用程序,具有明确的授权请求(出于某种原因,我永远无法导入 dart:html 库)。

最好的解决方案是无缝完成,因为我会在应用程序启动时将内容存储在列表中,并在手动刷新按钮按下时重新读取。

我在这里找到了几个旧的解决方案,但其中描述的方法似乎不再起作用(从 4 到 5 年前)。

是否有关于将 Drive API 集成到用 dart 编写的 Flutter 应用程序的良好分步教程?

【问题讨论】:

  • 请提供具体信息,说明您尝试了什么以及这些尝试没有奏效(代码和错误消息)
  • 请参阅上面的编辑部分:这已经是一种新方法,至少可以构建,但也不会给出任何错误消息或结果。
  • 看来我确实遇到了一些错误,并且此方法不能用于打开远程文件:FileSystemException:无法打开文件,路径='drive.google.com/file/d/1UZWDwA2SXnizz__ZodlLp82MQCi1WnvM'(操作系统错误:没有这样文件或目录,errno = 2)
  • 你不能在 Flutter 中使用dart:htmldart:html 只能在浏览器应用程序中使用。 stackoverflow.com/questions/48477625/… 可能对 pub.dartlang.org/packages/googleapis 有帮助

标签: google-drive-api dart flutter


【解决方案1】:

我在这方面遇到了很多麻烦,这似乎比应该做的要困难得多。这也仅适用于 TXT 文件。您需要对其他文件使用 files.export()。

首先您需要获取文件列表。

ga.FileList textFileList = await drive.files.list(q: "'root' in parents");

那么你需要根据ID获取那些文件(这是TXT文件)

ga.Media response = await drive.files.get(filedId, downloadOptions: ga.DownloadOptions.FullMedia);

接下来是混乱的部分,您需要将您的 Media 对象流转换为 File,然后从中读取文本。 (@Google,请让这更容易。)

List<int> dataStore = []; 
    response.stream.listen((data) {  
     print("DataReceived: ${data.length}");
     dataStore.insertAll(dataStore.length, data);     
   }, onDone: () async {  
     Directory tempDir = await getTemporaryDirectory(); //Get temp folder using Path Provider
     String tempPath = tempDir.path;   //Get path to that location
      File file = File('$tempPath/test'); //Create a dummy file
      await file.writeAsBytes(dataStore); //Write to that file from the datastore you created from the Media stream
      String content = file.readAsStringSync(); // Read String from the file
      print(content); //Finally you have your text
      print("Task Done");  
   }, onError: (error) {  
     print("Some Error");  
   });  

【讨论】:

    【解决方案2】:

    目前没有很好的分步教程,但使用https://developers.google.com/drive/api/v3/manage-downloads 作为参考指南,通过https://pub.dev/packages/googleapis 在 Dart/Flutter 中使用哪些方法:下载或读取 Google Drive 文件的内容,您应该使用googleapis/Drive v3,或者特别是FilesResourceApi 类中的方法。

    • drive.files.export(),如果这是 Google 文档
      • /// Exports a Google Doc to the requested MIME type and returns the exported content. Please note that the exported content is limited to 10MB.
    • drive.files.get(),如果这是别的,一个非 Gdoc 文件
      • /// Gets a file's metadata or content by ID.

    简化示例:

    var drive = new DriveApi(http_client);
    
    drive.files.get(fileId).then((file) {
      // returns file
    });
    

    但是,我发现这个 Dart-GoogleAPIs 库似乎缺少一个等效于 executeMediaAndDownloadTo(outputStream) 的方法。在原始的 Google Drive API v3 中,此方法将 alt=media URL 参数添加到底层 HTTP 请求。否则,你会得到错误,这就是我所看到的:

    403,消息:导出需要 alt=media 来下载导出的 内容。

    而且我无法找到另一种方法将该 URL 参数插入当前请求(也许其他人知道?)。因此,作为替代方案,您将不得不求助于实现自己的 Dart API 来做同样的事情,正如这个 OP 在此处所做的事情所暗示的那样 https://github.com/dart-lang/googleapis/issues/78: CustomDriveApi

    所以你要么:

    1. 通过 Dart 使用你自己的 HttpClient 实现并尝试密切关注 REST flow from Dart-GoogleAPIs,但请记住包含 alt=media
    2. implement and integrate您自己的原生Android/iOS代码并使用原始SDK方便的executeMediaAndDownloadTo(outputStream)

    (请注意,我没有测试 googleapis/Drive v2,但快速检查相同的方法看起来它们缺少相同的东西)

    【讨论】:

      猜你喜欢
      • 2023-04-05
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多