Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to download file created by my app on Google Drive, But can get the metadata of that file

I followed all the steps mentioned in google drive sdk. I created a sample application on my device(android, running jelly bean) and am able to upload a file on to drive. When trying to download the same file, I am able to get the meta data like fileID, fileTitle, fileDownloadURL etc but not able to download the content. I get 401 Unauthorized error.

My app AUTH SCOPE is AUTH_TOKEN_TYPE = "oauth2:https://www.googleapis.com/auth/drive.file";

I am doing the following to get the OAUTH Token:

AccountManager am = AccountManager.get(this);
        Bundle options = new Bundle();
        am.getAuthToken(
                mAccount,                               
                AUTH_TOKEN_TYPE,                        
                options,                                
                this,                                   
                new OnTokenAcquired(),                  
                new Handler(){
                    @Override
                    public void handleMessage(Message msg) {
                        invadiateToken();                       
                        super.handleMessage(msg);
                    }
                });  

Based on the token this is how I build the Drive object

Drive buildService(final String AuthToken) {
    HttpTransport httpTransport = new NetHttpTransport();
    JacksonFactory jsonFactory = new JacksonFactory();

    Drive.Builder b = new Drive.Builder(httpTransport, jsonFactory, null);
    b.setJsonHttpRequestInitializer(new JsonHttpRequestInitializer() {

        @Override
        public void initialize(JsonHttpRequest request) throws IOException {
            DriveRequest driveRequest = (DriveRequest) request;
            driveRequest.setPrettyPrint(true);
            driveRequest.setKey(API_KEY);
            driveRequest.setOauthToken(AuthToken);
        }
    });

    return b.build();
}

I am able to upload the file using the following code:

private void uploadLocalFileToDrive(Drive service) throws IOException{

        // File's metadata.
        String mimeType = "text/plain";
        File body = new File();
        body.setTitle("myText.txt");
        body.setDescription("sample app by varun");
        body.setMimeType("text/plain");

        // File's content.
        java.io.File fileContent = new java.io.File(mInternalFilePath);
        FileContent mediaContent = new FileContent(mimeType, fileContent);

        service.files().insert(body, mediaContent).execute();

    }

While trying to download the same file uploaded by this app, I get a 401 unauthorized error at this line HttpResponse resp = service.getRequestFactory().buildGetRequest(url).execute() from the following code snippet

private void downloadFileFromDrive(Drive service) throws IOException {
        Files.List request;
            request = service.files().list();
            do {
                FileList files = request.execute(); 
                for(File file:files.getItems()){
                    String fieldId = file.getId();
                    String title = file.getTitle();
                    Log.e("MS", "MSV::  Title-->"+title+"  FieldID-->"+fieldId+" DownloadURL-->"+file.getDownloadUrl());
                    if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0 ) {
                        GenericUrl url = new GenericUrl(file.getDownloadUrl());
                        HttpResponse resp = service.getRequestFactory().buildGetRequest(url).execute();
                        InputStream isd = resp.getContent();
                        Log.e("MS", "MSV:: FileOutPutStream--->"+getFilesDir().getAbsolutePath()+"/downloaded.txt");

                    } else {
                        Log.e("MS", "MSV:: downloadURL for this file is null");
                    }
                }
                request.setPageToken(files.getNextPageToken());
            } while (request.getPageToken()!=null && request.getPageToken().length()>0);
    }

Can anyone help me out and let me know what I am doing wrong???

like image 252
Varun.MS Avatar asked Feb 20 '23 20:02

Varun.MS


1 Answers

This is a known issue that will be resolved with the release of the Google Play Services APIs.

Since your application is authorized for the https://www.googleapis.com/auth/drive.file scope, and the download endpoint doesn't support the ?key= query parameter, there is no way for our server to know which project is issuing the request (to make sure the app has authorization to read this file's content).

In the meantime, the only workaround I can recommend is using the broad scope: https://www.googleapis.com/auth/drive. Please use only that while developing your application and waiting for the Google Play Services to be released.

To learn more about how you will be able to use the new authorization APIs in Android, you might be interested in those 2 Google I/O talks: Building Android Applications that Use Web APIs and Writing Efficient Drive Apps for Android

like image 74
Alain Avatar answered Apr 07 '23 01:04

Alain