Hazem Farahat
Hazem Farahat

Reputation: 3790

Downloading 'App data' files from Google Drive using the Android Drive SDK

I had the google drive sdk quick start for android up and running.

What I am trying to do is to expand the application to be able to upload a file on the drive new 'App Data' folder and be able to download this file back again (Sync config files between android devies)

Uploading a file works fine. But When I try to download the same file, I can get its meta data like fileID, fileTitle, fileDownloadURL etc but can not download the the contents. I receive 401 Unauthorized error while trying.

I am usnig AUTH SCOPE "https://www.googleapis.com/auth/drive.appdata" to access appdata.

Any Idea why this is happening?

Upvotes: 3

Views: 2068

Answers (2)

Moktahid Al Faisal
Moktahid Al Faisal

Reputation: 910

try the following code to retrieve contents

public void retrieveContents(DriveFile file) {
        // [START drive_android_open_file]
        Task<DriveContents> openFileTask =
                getDriveResourceClient().openFile(file, DriveFile.MODE_READ_ONLY);
        // [END drive_android_open_file]
        // [START drive_android_read_contents]


        openFileTask.continueWithTask(new Continuation<DriveContents, Task<Void>>() {
            @Override
            public Task<Void> then(@NonNull Task<DriveContents> task) throws Exception {
                DriveContents contents = task.getResult();
                // Process contents...
                // [START_EXCLUDE]
                // [START drive_android_read_as_string]
                try (BufferedReader reader = new BufferedReader(
                        new InputStreamReader(contents.getInputStream()))) {
                    StringBuilder builder = new StringBuilder();
                    String line;
                    while ((line = reader.readLine()) != null) {
                        builder.append(line).append("\n");
                    }

                    Log.e("result ", builder.toString());
                }
                // [END drive_android_read_as_string]
                // [END_EXCLUDE]
                // [START drive_android_discard_contents]
                Task<Void> discardTask = MainActivity.this.getDriveResourceClient().discardContents(contents);
                // [END drive_android_discard_contents]
                return discardTask;
            }
        })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {

                    }
                });

        // [END drive_android_read_contents]
    }

Upvotes: 0

Ali Afshar
Ali Afshar

Reputation: 41633

I can reproduce this when only using the drive.appdata scope. If I add another Drive scope like the drive.readonly scope it works fine.

I agree this is a bug (or at least surprising behavior), so I will raise one, but please use the workaround above for now.

Upvotes: 3

Related Questions