简体   繁体   English

Flutter - 如何使用 Google Drive API 下载公共文件

[英]Flutter - How to download public files using Google Drive API

I'm trying to use for the first time a Google API, Google Drive API, I'm authenticating the user with Google SignIn.我第一次尝试使用 Google API、Google Drive API,我正在使用 Google SignIn 对用户进行身份验证。 I created a ClientId, ClientSecret and Scope after enabling Google Drive API, but I don't know how to use them.我在启用 Google Drive API 后创建了 ClientId、ClientSecret 和 Scope,但我不知道如何使用它们。 I ended up creating a HTTP client using the GoogleSignInAccount's authHeaders.我最终使用 GoogleSignInAccount 的 authHeaders 创建了一个 HTTP 客户端。

I won't need to access my files or manage them, I just need to be authenticated because I might need to download big files, using direct links doesn't work since it has a confirmation window for big files that can't be scanned.我不需要访问我的文件或管理它们,我只需要通过身份验证,因为我可能需要下载大文件,使用直接链接不起作用,因为它有一个无法扫描的大文件的确认窗口.

When I try do download a public file using it's ID I always get the error below.当我尝试使用它的 ID 下载公共文件时,我总是收到以下错误。

class GoogleHttpClient extends IOClient {
  Map<String, String> _headers;

  GoogleHttpClient(this._headers) : super();

  @override
  Future<IOStreamedResponse> send(BaseRequest request) =>
      super.send(request..headers.addAll(_headers));

  @override
  Future<Response> head(Object url, {Map<String, String> headers}) =>
      super.head(url, headers: headers..addAll(_headers));

}

class GDriveHelper {

  // NOT USING
  //static final clientId = "??????.apps.googleusercontent.com";
  //static final clientSecret = "??????";
  //static final scopes = [ga.DriveApi.DriveFileScope];

  static UserController userController = Get.find();

  static Future<void> download(String fileId) async {

    Map<String, String> headers = await userController.googleSignInAccount.authHeaders;

    var client = GoogleHttpClient(headers);
    var drive = ga.DriveApi(client);

    await drive.files.get(fileId).then((file){

      print("FILE: $file");

    });



  }

}

When I call the method I get this error:当我调用该方法时,出现此错误:

await GDriveHelper.download(fileId);
            
DetailedApiRequestError(status: 403, message: Insufficient Permission: Request had insufficient authentication scopes.)

From your question, I could understand that you try to download a publicly shared file.从您的问题中,我可以理解您尝试下载公开共享的文件。 And from our discussions, I confirmed that clientId , clientSecret and scopes are not used.从我们的讨论中,我确认没有使用clientIdclientSecretscopes From these situation, as a simple method, I would like to propose to downloading the publicly shared file using the API key.从这些情况来看,作为一个简单的方法,我想建议使用 API 密钥下载公开共享的文件。 The API key can download the publicly shared file. API 密钥可以下载公开共享的文件。 And the request can be simple as follows.请求可以很简单,如下所示。

Sample request:样品请求:

GET https://www.googleapis.com/drive/v3/files/[FILEID]?alt=media&key=[YOUR_API_KEY]
  • In this method, the files except for Google Docs (Document, Spreadsheet, Slides and so on) can be downloaded by the method of "Files: get" in Drive API.在该方法中,除了Google Docs(文档、电子表格、幻灯片等)之外的文件都可以通过Drive API 中 的“文件:获取”方法下载。

  • When you want to download the Google Docs files, please use the method of "Files: export" as follows.当您要下载 Google Docs 文件时,请使用“文件:导出”的方法如下。

     GET https://www.googleapis.com/drive/v3/files/[FILEID]/export?mimeType=[mimeType]&key=[YOUR_API_KEY]

Sample curl command:示例 curl 命令:

As a test, when you use curl, the sample curl command is as follows.作为测试,当您使用 curl 时,示例 curl 命令如下。

Files except for Google Docs files: 除 Google Docs 文件外的文件:
 curl "https://www.googleapis.com/drive/v3/files/[FILEID]?alt=media&key=[YOUR_API_KEY]"
Google Docs files: 谷歌文档文件:
 curl "https://www.googleapis.com/drive/v3/files/[FILEID]/export?mimeType=[mimeType]&key=[YOUR_API_KEY]"

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 API flutter 覆盖谷歌驱动器中的文件 - How to overwrite files in google drive using API flutter 如何在flutter应用程序中使用google drive api创建文件夹? - How to create folder using google drive api in a flutter app? Flutter 如何使用谷歌驱动器 api 获取将文件上传到谷歌驱动器的进度状态? - Flutter How can I get progress status of uploading file to google drive by using google drive api? Google Drive API,从帐户服务器下载和获取内容文件 - Google Drive API, download and get content files from a account server 如何在 flutter web 中将文件从谷歌驱动器直接下载到本地? - How to direct download file from google drive to local in flutter web? 如何使用 Flutter 将文件上传到 Google Drive? - How do I upload a file to Google Drive using Flutter? Flutter 从 Google Drive 和 One drive 下载部分文件 - Flutter downloads partial files from Google Drive and One drive Flutter App 无法使用 google_sign_in 包向 Google Drive API 进行身份验证 - Flutter App Cant Authenticate to Google Drive API Using google_sign_in Package Dart / flutter:下载或读取 Google Drive 文件的内容 - Dart / flutter: download or read the contents of a Google Drive file 尝试 Google Drive 部分下载 (Flutter) 会引发标头错误 - Attempting a Google Drive partial Download (Flutter) throws a header error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM