Search code examples
swiftgoogle-drive-api

How to get a list of files with shared-link using GoogleAPIClientForREST [Swift]


I am building an app where users can download files with Google Drive's shared-link.

Trying to get a list of files with shared folder link, but couldn't find any document explaining how to do that.

What I have tried so far

  1. to use GoogleAPIClientForREST 'fetchObject'
googleDriveService.fetchObject(with: url, objectClass: nil, executionParameters: nil) { ticket, any, e in
                print("thicket", ticket, any, e)
            }

(where I tested with 'link' as https://drive.google.com/drive/folders/1EVHGkbGwlFAi16DhVG4ZoYEng6UQ7_Km?usp=sharing) But it returns error below

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Got unexpected content type 'text/html; charset=utf-8''
  1. to use GoogleAPIClientForREST 'query'
let query = GTLRDriveQuery_FilesList.query()
        query.q = "'\(url)'"
        query.spaces = "drive"
        query.corpora = "user"
        
        googleDriveService.executeQuery(query) { (_, result, error) in
}

Which returns 'Invalid query' error.

  1. to use GoogleAPIClientForREST 'query' with fileID
guard let url = URL(string: text) else {
    return
}
googleDriveService.executeQuery(GTLRDriveQuery_FilesGet.query(withFileId: url.lastPathComponent)) { ticket, any, e in }

Which again, returns below error.

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Got unexpected content type 'text/html; charset=utf-8''

Is it possible to do this with GoogleAPIClientForREST?

If not, what would be the alternate?

If I need to provide more details, I am happy to do so.

Thank you for your time.


Solution

  • Solved it by myself.

    I could simply send query below using GoogleAPIClientForREST. It returns a list of files!

    let comps = sharedURL.components(separatedBy: "/")
    guard let folderID = comps.last?.components(separatedBy: "?").first else {
       failed()
       return
    }
    let query = GTLRDriveQuery_FilesList.query()
                    query.q = "'\(folderID)' in parents"
                    query.spaces = "drive"
                    query.corpora = "allDrives"
                    query.includeItemsFromAllDrives = true
                    query.supportsAllDrives = true