0
votes

I am trying to download file from google drive using the below mentioned code. I have successfully tested other methods like insert file, delete file upload file, but when trying to download the file I get 401 authorization error at HttpResponse response = request.execute() in the below code.

private static synchronized InputStream downloadFile(Drive service, File file) throws IOException 
    {
        //File file = service.files().get(fileId).execute();
        String fileURL = file.getDownloadUrl();
        GenericUrl genericUrl = new GenericUrl(fileURL);
        if (fileURL != null && fileURL.length() > 0) 
        {
            try {

                    HttpRequest request = service.getRequestFactory().buildGetRequest(genericUrl);
                    HttpResponse response = request.execute();
                    InputStream inStream = response.getContent();

                    //HttpResponse resp = service.getRequestFactory().buildGetRequest(url).execute();
                    System.out.println("content "+ inStream);
                    return inStream;
            } catch (IOException e) {

                // An error occurred.
                e.printStackTrace();
                System.out.println("downloadFile error :"+ e.getMessage());
                return null;
            }
        } else {
                    // The file doesn't have any content stored on Drive.
                    return null;
        }
    }

I have also tried implementing using Async, still i get 401 error

08-30 19:31:13.917: W/System.err(1585): com.google.api.client.http.HttpResponseException: 401 Unauthorized
08-30 19:31:13.925: W/System.err(1585):     at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:978)
08-30 19:31:13.935: W/System.err(1585):     at com.rise.risedrive.RiseDriveActivity$DownloadFile.doInBackground(RiseDriveActivity.java:112)
08-30 19:31:13.945: W/System.err(1585):     at com.rise.risedrive.RiseDriveActivity$DownloadFile.doInBackground(RiseDriveActivity.java:1)
08-30 19:31:13.955: W/System.err(1585):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-30 19:31:13.955: W/System.err(1585):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-30 19:31:13.965: W/System.err(1585):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-30 19:31:13.965: W/System.err(1585):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-30 19:31:13.975: W/System.err(1585):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-30 19:31:13.975: W/System.err(1585):     at java.lang.Thread.run(Thread.java:1019)
08-30 19:31:32.250: W/jdwp(1585): Debugger is telling the VM to exit with code=1
08-30 19:31:32.250: I/dalvikvm(1585): GC lifetime allocation: 2603 bytes

I have tried to debug it but couldn't help me solve this

Thanks in advance

1
Could you print the error message? You can find out more about how to catch error from this page. The credentials might have expired or the user might have revoked access to your app.Alain

1 Answers

0
votes

Nowhere in your code are you setting the Authorization header.

You need something like

setRequestProperty("Authorization", "OAuth " + "***ACCESS_TOKEN***");

For any 401/403 errors it's well worth getting your request working on the Oauth Playground https://developers.google.com/oauthplayground/ so you have a datum on which to build your app.