Listing of all folders of Google Drive through IOS

2020-07-16 03:17发布

问题:

Actually I integrated google-drive-sdk with my ios app. I can able to to upload specified file on google drive through google-drive-sdk for iOS. Additionally I want to provide a functionality for choosing a folder from available folder in which user want to upload that file on Google Drive.

So, I found how to list all files of Google Drive but unable to find how to list of all folders of Google Drive.

I also went through whole API reference on Google Developer site but didn't found any solution about it.

I had found somewhere that using below code folder listing can be done so tried it but it didn't work.

GTLQueryDrive *query = [GTLQueryDrive queryForFilesList];
query.q = @"mimeType='application/vnd.google-apps.folder' and trashed=false";

[self.driveService executeQuery:query completionHandler:^(GTLServiceTicket *ticket,
                                                          GTLDriveFileList *files,
                                                          NSError *error) {

    if (error == nil) {
        NSLog(@"Array of folder: %@", files.items);
    } else {
        NSLog(@"An error occurred: %@", error);
    }
}];

So is there any solution for getting list of folders from Google Drive using google-drive-sdk?

回答1:

Assuming this code works, there is a problem in the query. Multiple queries should be combined by and.

query.q = @"mimeType='application/vnd.google-apps.folder' and trashed=false";

For more examples of sample queries, take a look at Search for Files in official documentation.

Also, in case this code doesn't work, you want to use Files.list() with query above. Check the link and there is a sample code for Object-c you might want to use.



回答2:

What are the scopes you are using? make sure you are using

kGTLAuthScopeDrive = @"https://www.googleapis.com/auth/drive";

example:

NSString *scope = kGTLAuthScopeDrive;

GTMOAuth2ViewControllerTouch *authViewController =
[[GTMOAuth2ViewControllerTouch alloc] initWithScope:scope
                                           clientID:kClientId
                                       clientSecret:kClientSecret
                                   keychainItemName:kKeychainItemName
                                           delegate:self
                                   finishedSelector:finishedSelector];

[self presentViewController:authViewController animated:YES completion:nil];


回答3:

Use the proper scope.

limited drive.file scope that only allows it to access files that it created or that the user opened with it.

full drive scope that allows the app to manage all user's Drive files.

It is strongly recommended to request the limited scope when possible. For more details about all available scopes, check the Google Drive SDK documentation:

https://developers.google.com/drive/scopes