Flutter - 如何使用 Google Drive API 下载 public 文件
Flutter - How to download public files using Google Drive API
我第一次尝试使用 Google API、Google 驱动器 API,我正在使用 [=26= 验证用户] 登入。我在启用Google Drive API后创建了ClientId、ClientSecret和Scope,但我不知道如何使用它们。我最终使用 GoogleSignInAccount 的 authHeaders 创建了一个 HTTP 客户端。
我不需要访问我的文件或管理它们,我只需要进行身份验证,因为我可能需要下载大文件,使用直接链接不起作用,因为它有一个确认 window对于无法扫描的大文件。
当我尝试使用它的 ID 下载 public 文件时,我总是收到以下错误。
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");
});
}
}
当我调用该方法时出现此错误:
await GDriveHelper.download(fileId);
DetailedApiRequestError(status: 403, message: Insufficient Permission: Request had insufficient authentication scopes.)
根据你的问题,我了解到你试图下载一个公开共享的文件。从我们的讨论中,我确认未使用 clientId
、clientSecret
和 scopes
。从这些情况来看,作为一种简单的方法,我想建议使用 API 密钥下载公开共享的文件。 API 键可以下载公开共享的文件。并且请求可以简单如下。
示例请求:
GET https://www.googleapis.com/drive/v3/files/[FILEID]?alt=media&key=[YOUR_API_KEY]
此方法中,除Google Docs(Document、Spreadsheet、Slides等)外的文件都可以通过the method of "Files: get"下载到Drive API .
如果要下载Google文档文件,请使用the method of "Files: export"如下。
GET https://www.googleapis.com/drive/v3/files/[FILEID]/export?mimeType=[mimeType]&key=[YOUR_API_KEY]
curl 命令示例:
作为测试,当您使用 curl 时,示例 curl 命令如下。
Google 文档文件以外的文件:
curl "https://www.googleapis.com/drive/v3/files/[FILEID]?alt=media&key=[YOUR_API_KEY]"
Google 文档文件:
curl "https://www.googleapis.com/drive/v3/files/[FILEID]/export?mimeType=[mimeType]&key=[YOUR_API_KEY]"
参考文献:
我第一次尝试使用 Google API、Google 驱动器 API,我正在使用 [=26= 验证用户] 登入。我在启用Google Drive API后创建了ClientId、ClientSecret和Scope,但我不知道如何使用它们。我最终使用 GoogleSignInAccount 的 authHeaders 创建了一个 HTTP 客户端。
我不需要访问我的文件或管理它们,我只需要进行身份验证,因为我可能需要下载大文件,使用直接链接不起作用,因为它有一个确认 window对于无法扫描的大文件。
当我尝试使用它的 ID 下载 public 文件时,我总是收到以下错误。
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");
});
}
}
当我调用该方法时出现此错误:
await GDriveHelper.download(fileId);
DetailedApiRequestError(status: 403, message: Insufficient Permission: Request had insufficient authentication scopes.)
根据你的问题,我了解到你试图下载一个公开共享的文件。从我们的讨论中,我确认未使用 clientId
、clientSecret
和 scopes
。从这些情况来看,作为一种简单的方法,我想建议使用 API 密钥下载公开共享的文件。 API 键可以下载公开共享的文件。并且请求可以简单如下。
示例请求:
GET https://www.googleapis.com/drive/v3/files/[FILEID]?alt=media&key=[YOUR_API_KEY]
此方法中,除Google Docs(Document、Spreadsheet、Slides等)外的文件都可以通过the method of "Files: get"下载到Drive API .
如果要下载Google文档文件,请使用the method of "Files: export"如下。
GET https://www.googleapis.com/drive/v3/files/[FILEID]/export?mimeType=[mimeType]&key=[YOUR_API_KEY]
curl 命令示例:
作为测试,当您使用 curl 时,示例 curl 命令如下。
Google 文档文件以外的文件:curl "https://www.googleapis.com/drive/v3/files/[FILEID]?alt=media&key=[YOUR_API_KEY]"
Google 文档文件:
curl "https://www.googleapis.com/drive/v3/files/[FILEID]/export?mimeType=[mimeType]&key=[YOUR_API_KEY]"