Google Drive SDK Sharing A File

235 Views Asked by At

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 = @"[email protected]";
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
1

There are 1 best solutions below

0
On

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

  "emailAddress": "[email protected]",
  "role": "reader",
  "type": "user"

in Permissions.create, it should be work.