错误:使用objective-c将文件上传到Google云端硬盘

时间:2016-10-04 07:17:57

标签: objective-c google-drive-api

现在我正在尝试将文件上传到Google云端硬盘。 代码已成功构建,但我的文件未上传到Google云端硬盘。

这是我的代码。

-(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 Domain=com.google.HTTPStatus Code=501 "(null)"

请帮帮我。

1 个答案:

答案 0 :(得分:1)

基于此blog,如果客户端不是Web浏览器,则更有可能发生错误501 - 特别是如果Web服务器已旧。在任何一种情况下,如果客户端指定了有效的请求类型,则Web服务器响应不正确或者只是需要升级。

如此answer中所述,请确保您有权对这两项操作进行更改:

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

您还可以查看此相关主题: