Google Drive SDK Sharing A File

objective-c google-drive-api file-access

133 观看

1回复

40 作者的声誉

I am currently trying to use the google drive API for IOS to create a folder, put a file in the folder, and then share the file. Creating the folder, and putting the file in the folder works, but the sharing feature does not. When I log onto the email address that i shared the file with, it does not show up. Could someone please help me figure out where the issue would arise in the Objective -c code below.

The order the methods are called is initSetup where the folder is created, then in initsetup,uploadToFolder is called, an in uploadToFolder, the file is shared.

#import "userSetUp.h"
#import <GoogleSignIn/GoogleSignIn.h>
@import GoogleAPIClientForREST;


 @implementation userSetUp
 NSString *folderID;
-(id) initWithDriveService:(GTLRDriveService *)driveService{
self = [ super init];
if(self){
    self.driveService = driveService;
}
return self;
}
 - (void) initSetup{



GTLRDrive_File *metadata = [GTLRDrive_File object];
metadata.name = @"AName";
metadata.mimeType = @"application/vnd.google-apps.folder";
GTLRDriveQuery_FilesCreate *query = [GTLRDriveQuery_FilesCreate queryWithObject:metadata
                                                               uploadParameters:nil];
query.fields = @"id";
[_driveService executeQuery:query completionHandler:^(GTLRServiceTicket *ticket,
                                                     GTLRDrive_File *file,
                                                     NSError *error) {
    if (error == nil) {
        folderID =  file.identifier;
        //[self shareToDrive:file.identifier];
        //printf("the folder id is" + file.identifier )
        [self uploadToFolder: file.identifier];
        NSLog(@" FOLDER File ID %@", file.identifier);
    } else {
        NSLog(@"An error occurred: %@", error);
    }
}];




}
- (void) uploadToFolder:(NSString *) folderId {


NSString *filePath = [[NSBundle mainBundle] pathForResource:@"apple" ofType:@"jpg"];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
GTLRDrive_File *metadata = [GTLRDrive_File object];
metadata.name = @"apple.jpg";

//metadata.mimeType = @"application/vnd.google-apps.document";
metadata.parents = [NSArray arrayWithObject:folderId];

GTLRUploadParameters *uploadParameters = [GTLRUploadParameters uploadParametersWithData:fileData
                                                                               MIMEType:@"image/jpeg"];
uploadParameters.shouldUploadWithSingleRequest = TRUE;
GTLRDriveQuery_FilesCreate *query = [GTLRDriveQuery_FilesCreate queryWithObject:metadata
                                                               uploadParameters:uploadParameters];
query.fields = @"id";
[_driveService executeQuery:query completionHandler:^(GTLRServiceTicket *ticket,
                                                     GTLRDrive_File *file,
                                                     NSError *error) {
    if (error == nil) {
        /**
         *
         *This is where the file id of the file gets sent to be shared
         *
         */
        [self shareToDrive:file.identifier];
        NSLog(@"File ID %@", file.identifier);
    } else {
        NSLog(@"An error occurred: %@", error);
    }
}];
printf("uploaded image to folder");
}
- (void ) shareToDrive:(NSString *) fileId{
GTLRBatchQuery *batchQuery = [GTLRBatchQuery batchQuery];

GTLRDrive_Permission *userPermission = [GTLRDrive_Permission object];
userPermission.type = @"user";
userPermission.role = @"reader";
userPermission.emailAddress = @"anEmail@gmail.com";
GTLRDriveQuery_PermissionsCreate *createUserPermission =
[GTLRDriveQuery_PermissionsCreate queryWithObject:userPermission
                                           fileId:fileId];
createUserPermission.fields = @"id";
createUserPermission.completionBlock = ^(GTLRServiceTicket *ticket,
                                         GTLRDrive_Permission *permission,
                                         NSError *error) {
    if (error == nil) {
        printf("we out here");
        NSLog(@"Permisson ID: %@", permission.identifier);
    } else {
        //printf("oh no an error!");
        NSLog(@"An error occurred: %@", error);
    }

};
[batchQuery addQuery:createUserPermission];
printf("file shared");
}
@end
作者: JustANoob 的来源 发布者: 2017 年 9 月 15 日

回应 1


0

12709 作者的声誉

Don't know the ios codes exactly but I think as long as you specify the

  "emailAddress": "recipient@gmail.com",
  "role": "reader",
  "type": "user"

in Permissions.create, it should be work.

作者: noogui 发布者: 2017 年 9 月 16 日
32x32