W.venus W.venus - 15 days ago 7x
Objective-C Question

Error: Upload file to google drive using objective-c

Now I'm trying to upload file to Google Drive.
Code was built successfully, but my file didn't upload to Google Drive.

Here is my code.

-(void)uploadFileToGoogleDrive : (NSString*)fileName fileFullPath:(NSString*)filePath {

GTLDriveFile *driveFile = [GTLDriveFile object];

driveFile.mimeType = @"application/pdf";
driveFile.originalFilename = filePath;
driveFile.name = fileName;

NSString *parentId = @"root";
driveFile.parents = @[parentId];

NSData *fileContent = [[NSData alloc] initWithContentsOfFile:fileName];
GTLUploadParameters *uploadParameters = [GTLUploadParameters uploadParametersWithData:fileContent MIMEType:@"application/pdf"];

GTLQueryDrive *query = [GTLQueryDrive queryForFilesCreateWithObject:driveFile uploadParameters:uploadParameters];

[self.service executeQuery:query
completionHandler:^(GTLServiceTicket *ticket,
GTLDriveFile *updatedFile,
NSError *error) {
if (error == nil) {
NSLog(@"\nfile uploaded into google drive");
} else {
NSLog(@"\nAn error occurred: %@", error);

Error message is like this:

Error Domain=com.google.HTTPStatus Code=501 "(null)"

Please help me.


Based from this blog, error 501 occurs more likely if the client is not a Web browser - particularly if the Web server is old. In either case if the client has specified a valid request type, then the Web server is either responding incorrectly or simply needs to be upgraded.

As stated in this answer, make sure that you have permission to make changes in these two actions:

NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:self.fromPath];
        GTLUploadParameters *uploadParameters = [GTLUploadParameters uploadParametersWithFileHandle:fileHandle MIMEType:mimeType];
        GTLQueryDrive *uploadFileQuery = [GTLQueryDrive queryForFilesCreateWithObject:toFile

You can also check on this related threads: