Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing public Google Drive folder from Android app without authenticating

I'd like for my app to be able to read from a pre-defined shared public Google Drive folder without the user having to log in or choose a Google account.

Background / Environment

Using my desktop browser, I have created a public folder on my Google Drive that is set up to be public. Anyone with the link may access (read) the drive, so no authorization is required:

Desktop screen capture of shared Google Drive folder

In my Android Studio project, I have gone into File > Project Structure > Dependencies and added com.google.android.gms:play-services-drive:10.2.0

Android Studio image of Google Play Services as a module dependency

I now have the ability to create a new GoogleApiClient.Builder().

Question

I have looked at various examples, but in most cases, the drive has been created by the Android application. This is not the situation I'm trying to manage.

This question is about accessing a drive that has been made public using the "folder ID" or whatever you call 0B6X74x23H.... that was assigned when the folder was originally shared and made public.

I have examined the demo code provided by Google, but that, presumably, is not for a public folder because it says:

...need to register an OAuth 2.0 client

At a minimum, I could drive the process by using http-client, going to the sharing link https://drive.google.com/drive/folders/0B6X74x23Hx7DNE13M0ZIbVI....?usp=sharing with no authentication and not need to jump through hoops. But of course, it would be cleaner to use a defined API and simply specify the public shared folder in order to list the contents and, if needed, download the files from the public folder.

When I try this code:

        Scope publicFolder = new Scope(EXISTING_FOLDER_ID);
        mGoogleApiClient = new GoogleApiClient.Builder(mActivity)
                .addApi(Drive.API)
                .addScope(publicFolder)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
        mGoogleApiClient.connect();

This method fires:

GoogleApiClient.OnConnectionFailedListener.onConnectionFailed()

The result contains statusCode=SIGN_IN_REQUIRED. But of course, sign-in is NOT required for a folder that's public.

like image 981
Dale Avatar asked May 23 '17 19:05

Dale


People also ask

How do I access a shared folder on Google Drive app?

On the left hand side of your Google Drive look for link 'Share with me'. Click the link. The folder listing on the right hand side of your Google Drive will show all the folders and files that have been shared with you. Click on either the folder or file to open it up.

How do I access Google Drive folder on Android?

On your Android phone or tablet, open the Google Drive app. At the top, tap Search Drive. Choose from the following options: File types: Such as documents, images, or PDFs.


1 Answers

Here is a workaround with Jsoup

implementation 'org.jsoup:jsoup:1.11.3'

val url = "https://drive.google.com/drive/folders/xxxxxxxxxxxxxxxxxx"   // shared folder link
val doc = Jsoup.connect(url).get()
doc.outputSettings().prettyPrint(false)

val files = doc.select("div.WYuW0e")
for (file in files){
    val fileName = file.text()
    val fileID = file.attr("data-id")

    val downloadLink = "https://drive.google.com/uc?export=download&id=$fileID" 
    //the downloadLink may open a 'Google Drive can't scan this file for viruses' page
    
    // below we check for the new link  
    val doc2 = Jsoup.connect(downloadLink).get()
    doc2.outputSettings().prettyPrint(false)
    val elem = doc2.select("[id='uc-download-link']")
    val newLink = if (elem.size != 0){
        "https://drive.google.com" + elem.first().attr("href")
    } else {
        downloadLink
    }
}
like image 104
fbiego Avatar answered Sep 24 '22 00:09

fbiego