Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download Manager with Google Drive URL

I'm trying to download a file stored in Google Drive using android DownloadManager.

I get the sign in token from Google like following:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(AppConfig.getInstance().get("google_client_id"))
            .requestProfile()
            .requestEmail()
            .build();

I receive a notification with google drive file url and i pass it to the DownloadManager, passing to it the token:

String cookie = CookieManager.getInstance().getCookie(d.getURL());
            request.addRequestHeader("Cookie",cookie);
            request.addRequestHeader("Authorization", "OAuth " + profile.getToken());
//d is the document object, that contains url, file name, etcc
//Profile is a simple object class that hold the user data taken from sign Google, like token, name, email, etcc

Using a simple Broadcast Receiver to manage the download result (ACTION_DOWNLOAD_COMPLETE).

The download is done successfully, but the file is corrupted. If i try to open it, the device (and pc) gives me a format error. The file contains a HTML code of a Google page that says that there war an error, no more.

(The account that i'm using is enabled to read and dwonload the document form this specific drive storage)

Is this the correct way to download a Google Drive file using DownloadManager? Is it possible to do that?

like image 224
MikeKeepsOnShine Avatar asked Jul 20 '20 09:07

MikeKeepsOnShine


People also ask

Which Download Manager works with Google Drive?

You can use JDownloader 2 to directly download the file from Google Drive on Windows or Mac PC. Just paste the sharing link URL in the JDownloader and let the fetch the direct download link for you to download the file quickly.

Can you download a file from a Google Drive link?

Anyone can search for, find, and download the file. On (Link): Anyone with the link can download it. They don't have to sign in to their Google account to do so.


1 Answers

Try whether this helps...

As in @saddamkamal 's answer, use the Google Drive download URL.

AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                DownloadManager downloadmanager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                Uri uri = Uri.parse("https://drive.google.com/uc?id=<FILE_ID>&export=download");

                DownloadManager.Request request = new DownloadManager.Request(uri);
                request.setTitle("My File");
                request.setDescription("Downloading");
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "file.extension");
                downloadmanager.enqueue(request);
            }
        });
like image 101
Vishnu Avatar answered Oct 07 '22 01:10

Vishnu