2
votes

I integrated Google drive sdk with my iOS app. But I do not know how to upload a data(NSData type) to Google drive specific document(document's mimeType is 'application/vnd.google-apps.document and I want a upload with mixed data(image+text).)

Here the code. But this code is occurred error. please help me. -error message: Domain=com.google.GTLJSONRPCErrorDomain Code=400 (Bad Request)

NSMutableData *body = [NSMutableData data];
NSString *boundary = [NSString stringWithFormat:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];

GTLDriveFile *newFile = [GTLDriveFile object];
newFile.mimeType = @"application/vnd.google-apps.docuemnt";

// another text parameter
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"parameter2\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"body text2" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

// image file
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"imagename\"; filename=\"image.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:UIImageJPEGRepresentation([UIImage imageNamed:@"folder1"], 1.0)]];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

// text parameter
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"parameter1\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"body text" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

// close form
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];[body appendData:@""]; GTLUploadParameters *uploadParameters = nil;


uploadParameters = [GTLUploadParameters uploadParametersWithData:body MIMEType:contentType];
GTLQueryDrive *query = nil;
    // This is a new file, instantiate an insert query.
    query = [GTLQueryDrive queryForFilesInsertWithObject:newFile uploadParameters:uploadParameters];

query.convert = YES;

[self.driveService executeQuery:query completionHandler:^(GTLServiceTicket *ticket,
                                                          GTLDriveFile *updatedFile,
                                                          NSError *error)
 {..}
2

2 Answers

0
votes

The mime type you've specified: application/vnd.google-apps.document is for Google Docs documents. You shouldn't be touching those. You want to create your own document instead.

0
votes

Just to be sure: did you try with the correct mime-type? There is a typo in your post: "application/vnd.google-apps.docuemnt", which is not an acceptable mime-type for their API