[英]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.从我们的讨论中,我确认没有使用
clientId
、 clientSecret
和scopes
。 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.
请求可以很简单,如下所示。
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]
As a test, when you use curl, the sample curl command is as follows.作为测试,当您使用 curl 时,示例 curl 命令如下。
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]"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.