我已经成功将Google云端硬盘完全集成到我的应用中,但是我会坚持在Google云端硬盘上保存和获取图片
我写了以下代码
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"'Quickstart Uploaded File ('EEEE MMMM d, YYYY h:mm a, zzz')"];
GTLDriveFile *file = [GTLDriveFile object];
file.title = [dateFormat stringFromDate:[NSDate date]];
file.descriptionProperty = @"Uploaded from the Google Drive iOS Quickstart";
file.mimeType = @"image/png";
UIImage *imagenda=[UIImage imageNamed:@"back_button.png"];
// NSData *data = UIImagePNGRepresentation(imagenda, 1.0);
// add image data
NSData *data = UIImageJPEGRepresentation(imagenda, 1.0);
GTLUploadParameters *uploadParameters = [GTLUploadParameters uploadParametersWithData:data MIMEType:file.mimeType];
GTLQueryDrive *query = [GTLQueryDrive queryForFilesInsertWithObject:file
uploadParameters:uploadParameters];
// UIAlertView *waitIndicator = [self showWaitIndicator:@"Uploading to Google Drive"];
[driveService executeQuery:query
completionHandler:^(GTLServiceTicket *ticket,GTLDriveFile *insertedFile, NSError *error)
{
//[waitIndicator dismissWithClickedButtonIndex:0 animated:YES];
if (error == nil)
{
NSLog(@"File ID: %@", insertedFile.identifier);
[self showAlert:@"Google Drive" message:@"File saved!"];
}
else
{
NSLog(@"An error occurred: %@", error);
[self showAlert:@"Google Drive" message:@"Sorry, an error occurred!"];
}
}];
最佳答案
您需要添加范围:-
// Authorization scope
NSString * const kGTLAuthScopeDrive = @"https://www.googleapis.com/auth/drive";
NSString * const kGTLAuthScopeDriveAppdata = @"https://www.googleapis.com/auth/drive.appdata";
NSString * const kGTLAuthScopeDriveAppsReadonly = @"https://www.googleapis.com/auth/drive.apps.readonly";
NSString * const kGTLAuthScopeDriveFile = @"https://www.googleapis.com/auth/drive.file";
NSString * const kGTLAuthScopeDriveMetadataReadonly = @"https://www.googleapis.com/auth/drive.metadata.readonly";
NSString * const kGTLAuthScopeDriveReadonly = @"https://www.googleapis.com/auth/drive.readonly";
NSString * const kGTLAuthScopeDriveScripts = @"https://www.googleapis.com/auth/drive.scripts";
// Creates the auth controller for authorizing access to Drive API.
- (GTMOAuth2ViewControllerTouch *)createAuthController {
GTMOAuth2ViewControllerTouch *authController;
NSArray *scopes = [NSArray arrayWithObjects:kGTLAuthScopeDriveFile, nil];
authController = [[GTMOAuth2ViewControllerTouch alloc]
initWithScope:[scopes componentsJoinedByString:@" "]
clientID:kClientID
clientSecret:kClientSecret
keychainItemName:kKeychainItemName
delegate:self
finishedSelector:@selector(viewController:finishedWithAuth:error:)];
return authController;
}
关于ios - 如何在Google云端硬盘上保存和检索图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32561972/