【发布时间】:2016-05-02 02:30:19
【问题描述】:
我尝试从 google drive API 下载文件 3 天后没有成功。我用过这个https://developers.google.com/drive/ios/devguide/files#reading_files。
但我不明白我需要在 *drive 和 *file 中放入什么?
我试过了:GTLDriveFile *file = @"fileText.txt";(或者我尝试了我在谷歌驱动器上的文件的网址......)指南没有解释......而且我没有找到真实的例子。
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);
}
}];
所以我搜索了其他代码,但没有人解释我需要在驱动器和文件中放入什么:
- how to download file from google drive using objective c?(就说是网址)
- Google drive api download file for iOS
- IOS: How to Download Google Docs files using ios google drive sdk API?
解决方案:
我的示波器存在授权问题,通过完全访问驱动器解决了问题。我更改了范围(在快速入门代码中,查看:“- (GTMOAuth2ViewControllerTouch *)createAuthController...”)
-->NSArray *scopes = [NSArray arrayWithObjects:kGTLAuthScopeDrive, nil];
供下载(受快速入门示例启发):
// self.service is my GTLServiceDrive
// When the view appears, ensure that the Drive API service is authorized, and perform API calls.
- (void)viewDidAppear:(BOOL)animated {
if (!self.service.authorizer.canAuthorize) {
// Not yet authorized, request authorization by pushing the login UI onto the UI stack.
[self presentViewController:[self createAuthController] animated:YES completion:nil];
} else {
NSString *urltest = [NSString stringWithFormat:@"https://www.googleapis.com/drive/v3/files/%@?alt=media", identifier_file]; //the ID of my file in a string identifier_file
GTMSessionFetcher *fetcher = [self.service.fetcherService fetcherWithURLString:urltest]; // the request
// receive response and play it in web view:
[fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *errorrr) {
if (errorrr == nil) {
NSLog(@"Retrieved file content");
[webView_screen loadData:data MIMEType:@"application/pdf" textEncodingName:@"UTF-8" baseURL:nil]; //my file is a pdf
[webView_screen reload];
} else {
NSLog(@"An error occurred: %@", errorrr);
}
}];
}
}
如果你想保存在手机上,你可以看看巴拉的代码。
【问题讨论】:
-
发生了哪些类型的错误?
-
问题更多,我不知道我需要在 *drive 和 *file 中放入什么:一个标识?什么类型,什么标识?我在哪里可以找到它? (是驱动器中文件的名称?但是这段代码如何在我的google驱动器的good文件夹中找到好文件?(我不明白这个协议的功能......)
标签: ios objective-c google-drive-api