上传

时间:2017-08-16 04:33:24

标签: objective-c google-drive-api

使用适用于iOS的Quickstart应用,我可以获取Google云端硬盘浏览文件和文件夹,显示图标和缩略图以及下载文件。但是,当涉及到上传时,我收到以下错误:

发生错误:错误域= com.google.GTLRErrorObjectDomain代码= 403“权限不足”UserInfo = {GTLRStructuredError = GTLRErrorObject 0x61800024ff90:{message:“Insufficient Permission”错误:[1]代码:403}, NSLocalizedDescription =权限不足}

从这里和Google论坛上的评论看来,我似乎需要使用云端硬盘范围,但我无法弄清楚如何做到这一点。这是来自登录的回调:

- (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user withError:(NSError *)error {
    if (error != nil) {
        [self showAlert:@"Authentication Error" message:error.localizedDescription];
        _driveService.authorizer = nil;
    } else {
        self.signInButton.hidden = true;
        _driveService.authorizer = user.authentication.fetcherAuthorizer;
        [self signedIn];
        [self listFilesFromFolderID:[self initialFolder]];
    }
}

然后,要上传我正在执行此操作的文件:

_driveFile = [GTLRDrive_File object];
_driveFile.name = [NSString stringWithFormat:@"%@.%@", _filenameToSave, _fileExtension];
_driveFile.mimeType = _fileMimeType;

NSString *parentRef = @"root";

if ( self.currentFolder )
    parentRef = [self.currentFolder.identifier copy];

_driveFile.parents = @[ parentRef ];

GTLRUploadParameters *uploadParameters = [GTLRUploadParameters uploadParametersWithData:fileData
                                                                               MIMEType:_driveFile.mimeType];
uploadParameters.shouldUploadWithSingleRequest = TRUE;
GTLRDriveQuery_FilesCreate *query = [GTLRDriveQuery_FilesCreate queryWithObject:_driveFile
                                                               uploadParameters:uploadParameters];
query.fields = @"id";
[self.driveService executeQuery:query completionHandler:^(GTLRServiceTicket *ticket,
                                                     GTLRDrive_File *file,
                                                     NSError *error) {
    if (error == nil) {
        NSLog(@"File ID %@", file.identifier);
    } else {
        NSLog(@"An error occurred: %@", error);
    }
}];

1 个答案:

答案 0 :(得分:9)

在向Google注册SignIn时,问题是获得了许可。请检查您在登录时提供的范围

GIDSignIn* signIn = [GIDSignIn sharedInstance];
signIn.delegate = self;
signIn.uiDelegate = self;
signIn.scopes = [NSArray arrayWithObjects:kGTLRAuthScopeDrive, nil];

的变化:

kGTLRAuthScopeDriveReadonly --> kGTLRAuthScopeDrive

我把它放在kGTLRAuthScopeDriveReadonly并得到同样的错误。只需查看相同的代码即可。