如何在Google云端硬盘上保存和检索图片

时间:2015-09-14 09:59:31

标签: ios object google-drive-api

我已成功将谷歌硬盘整合到我的应用程序中,但我会坚持保存并在谷歌硬盘上获取图像 我写了以下代码

 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!"];
         }
     }];

1 个答案:

答案 0 :(得分:1)

您需要添加范围: -

// 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;
}