Google云端硬盘-403没有足够的权限上传文件,但没有创建文件夹

时间:2019-12-09 12:59:43

标签: ios objective-c google-drive-api

我正在尝试使用私有应用程序数据文件夹将Google云端硬盘支持添加到我的一个应用程序中。我使用GIDSignIn类进行登录,并将作用域设置为kGTLRAuthScopeDriveAppdata。登录后,我可以创建文件夹并获得显示文件夹存在的文件列表,然后可以删除文件夹,文件列表显示它们已经消失。但是由于某种原因,当我尝试上传文件时,出现403错误(“用户没有足够的权限访问此文件。”)。无论我尝试将文件放在应用程序数据文件夹的根目录还是我创建的文件夹中,都会发生这种情况。 我已经在Google Developer Console中设置了一个项目。我已将API密钥配置为可与我的应用程序的捆绑软件ID一起使用,并赋予其不受限制的API访问权限。 Google Drive API已启用。

我的代码改编自Google自己的示例,因此其中很多看起来都很熟悉。我已经简化了登录处理,因为这似乎工作正常。

start cmd /c prp-sql

作为示例,我尝试在GIDSignIn登录后执行此操作:

- (instancetype) init
{
    self = [super init];
    if (!self) return nil;

    [GIDSignIn sharedInstance].clientID = (NSString *)kGoogleClientId;
        //kGoogleClientId is the ID from the developer console.
    [GIDSignIn sharedInstance].delegate = self;
    [GIDSignIn sharedInstance].scopes = @[kGTLRAuthScopeDriveAppdata];

    return self;
}


//GIDSignInDelegate method
- (void) signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user withError:(NSError *)error
{   
    authenticatedUser = user;   //authenticatedUser is an instance variable
    NSLog(@"Signed in to Google Drive with user %@", user.profile.name);
    [delegate GoogleDriveDidSignIn:self];
}


- (GTLRDriveService *) driveService
{
    static GTLRDriveService *service;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,
           ^{
            service = [[GTLRDriveService alloc] init];
            service.APIKey = (NSString *)kGoogleApiKey;
                //kGoogleApiKey matches the developer console too. It has unrestricted API access and is tied to my bundle ID
            service.APIKeyRestrictionBundleID = [[NSBundle mainBundle] bundleIdentifier];

            service.shouldFetchNextPages = YES;
            service.retryEnabled = YES;
            });

    service.authorizer = authenticatedUser.authentication.fetcherAuthorizer;
        //authenticatedUser is an instance variable which stores the user information returned by
        //GIDSignIn when the user signs in

    return service;
}


- (void) createFolderNamed:(NSString *)folderName completionHandler:(void(^)(NSString *foldername, NSString *newFolderId))completionHandler
{
    GoogleDriveHandler * __weak weakself = self;
    GTLRDriveService *service = [self driveService];
    GTLRDrive_File *folder = [GTLRDrive_File object];
    folder.name = folderName;
    folder.mimeType = (NSString *)kMimeType_GoogleDriveFolder;
    folder.parents = @[@"appDataFolder"];
    GTLRDriveQuery_FilesCreate *query = [GTLRDriveQuery_FilesCreate queryWithObject:folder uploadParameters:nil];
    [service executeQuery:query completionHandler:^(GTLRServiceTicket * _Nonnull callbackTicket, id  _Nullable object, NSError * _Nullable callbackError)
        {
        if (callbackError)
            {
            NSLog(@"-createFolderNamed: callbackError: %@", callbackError.localizedDescription);
            }
        else
            {
            GTLRDrive_File *createdFolder = (GTLRDrive_File *)object;
            if ( [createdFolder.mimeType isEqualToString:(NSString *)kMimeType_GoogleDriveFolder] )
                {
                NSLog(@"Google Drive created folder named \"%@\" with identifier \"%@\" and mime-type \"%@\"", createdFolder.name, createdFolder.identifier, createdFolder.mimeType);
                }
            else
                {
                NSLog(@"Error : Attempted to create folder, but Google Drive created item named \"%@\" with identifier \"%@\" and mime-type \"%@\"", createdFolder.name, createdFolder.identifier, createdFolder.mimeType);
                }
            }
        }];
}


- (void) writeFileAtUrl:(NSURL *)source toFolderWithId:(NSString *)folderId completionHandler:(void(^)(NSString *filename, NSString *newFileId))completionHandler
{
    GoogleDriveHandler * __weak weakself = self;
    GTLRDriveService *service = [self driveService];
    GTLRDrive_File *file = [GTLRDrive_File object];
    file.name = source.lastPathComponent;
    file.mimeType = @"binary/octet-stream";
    file.parents = @[folderId];
    file.spaces = @[@"appDataFolder"];

    GTLRUploadParameters *parameters = [GTLRUploadParameters uploadParametersWithFileURL:source MIMEType:@"binary/octet-stream"];

    parameters.shouldUploadWithSingleRequest = YES;
    GTLRDriveQuery_FilesCreate *query = [GTLRDriveQuery_FilesCreate queryWithObject:file uploadParameters:parameters];
    query.fields = @"id";

    [service executeQuery:query completionHandler:^(GTLRServiceTicket * _Nonnull callbackTicket, id  _Nullable object, NSError * _Nullable callbackError)
            {
            if (callbackTicket.statusCode == 200)
                {
                GTLRDrive_File *createdFile = (GTLRDrive_File *)object;
                NSLog(@"Wrote file %@ in Google Drive folder %@", createdFile.name, folderId);
                if (completionHandler) completionHandler(createdFile.name, createdFile.identifier);
                }
            else
                {
                NSLog(@"-writeFileAtUrl:toFolderWithId:completionHandler:  status code = %li : callbackError: %@", callbackTicket.statusCode, callbackError.localizedDescription);
                }
            }];
}

我仍然收到403错误。 在这一点上,我已经阅读了几种不同编程语言的大量教程,博客文章和论坛主题。我已经遍历了自己的代码几次,并添加了大量的日志记录语句来仔细检查所有内容,但是我无法弄清楚如何拥有创建文件夹的权限,但不能在其中放置文件。


稍后,...
如果您通过Google控制台中的凭据向导(而不是因为正在创建iOS应用程序而仅选择iOS凭据),则会收到一条消息,内容为“无法从iOS安全访问应用程序数据。请考虑选择其他平台”,它拒绝为您创建证书。尽管SDK具有必需的常量,但这有可能不起作用吗?

1 个答案:

答案 0 :(得分:0)

对于那些追随我的人,我认为我得出的结论是,在iOS中使用appDataFolder根本行不通。 切换到使用驱动器空间中的文件夹后,我还发现GTLRUploadParameters的-uploadParametersWithFileURL:MIMEType:方法不起作用。使用该驱动器时,会在驱动器的根目录中得到一个名为“无标题”的文件(包含在GTLRDrive_File对象中设置的文件元数据)。切换到-uploadParametersWithData:MIMEType:后,我会在正确的位置获得正确的文件。 我想到目前为止的教训是,如果某些问题不起作用,请假设它是SDK。