通过这种方式,我成功下载了一张图片:
GTLServiceDrive *drive = ...;
GTLDriveFile *file = ...;
NSString *url = [NSString stringWithFormat:@"https://www.googleapis.com/drive/v3/files/%@?alt=media",file.identifier];
GTMSessionFetcher *fetcher = [drive.fetcherService fetcherWithURLString:url];
[fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
if (error == nil) {
NSLog(@"Retrieved file content");
// Do something with data
} else {
NSLog(@"An error occurred: %@", error);
}
}];
google drive sdk的文档告诉我下载pdf或google文档应该使用其他网址:
NSString *url = [NSString stringWithFormat:@"https://www.googleapis.com/drive/v3/files/%@/export?alt=media&mimeType=application/pdf",file.identifier];
但我失败了,错误就是这样:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "badRequest",
"message": "Bad Request"
}
],
"code": 400,
"message": "Bad Request"
}
}
答案 0 :(得分:0)
在下载文件时,请确保您的应用必须使用允许读取文件内容的范围进行授权。例如,使用drive.readonly.metadata范围的应用程序无权下载文件内容。具有编辑权限的用户可以通过将viewersCanCopyContent字段设置为true来限制只读用户的下载。
使用部分下载还有另一种下载文件的方法。它涉及仅下载文件的指定部分。您可以使用带有Range标头的字节范围指定要下载的文件部分。
此SO question正在使用部分下载。我认为它可以帮助你。
此代码演示了如何下载PDF格式的Google文档。
String fileId = "1ZdR3L3qP4Bkq8noWLJHSr_iBau0DNT4Kli4SxNc2YEo";
OutputStream outputStream = new ByteArrayOutputStream();
driveService.files().export(fileId, "application/pdf")
.executeMediaAndDownloadTo(outputStream);