简体   繁体   中英

Pick any file from users Google Drive and send (Upload) it to my server via Android app

I am using the Google Drive Android API (GDAA) and I want to enable the user to pick any file (Google Docs, Sheets, Images, Video, Audio etc.) from their Drive and send it to the app server.

I have followed several tutorials on the web and also the official Google documentation for using the GDAA.

I tried the following answer as well: Fetching contents of Google Drive file through FileId

But this too does not work. I get result.getStatus().isSuccess() as false always. I do not understand why.

The following is my open() function:

private void open() {
    DriveFile driveFile = mFileId.asDriveFile();
    driveFile.open(mGoogleApiClient, DriveFile.MODE_READ_ONLY, null)
            .setResultCallback(driveContentsCallback);
    mFileId = null;
}

And the following is the callback where I get result.getStatus().isSuccess() as false :

/**
 * This is Result result handler of Drive contents.
 */
final ResultCallback<DriveApi.DriveContentsResult> driveContentsCallback =
        new ResultCallback<DriveApi.DriveContentsResult>() {
            @Override
            public void onResult(DriveApi.DriveContentsResult result) {

                if (!result.getStatus().isSuccess()) {
                    return;
                }

                // Read from the input stream an print to LOGCAT
                DriveContents driveContents = result.getDriveContents();
                BufferedReader reader = new BufferedReader(new InputStreamReader(driveContents.getInputStream()));
                StringBuilder builder = new StringBuilder();
                String line;
                try {
                    while ((line = reader.readLine()) != null) {
                        builder.append(line);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                String contentsAsString = builder.toString();
                Log.i("ContentAsString", contentsAsString);

                // Close file contents
                driveContents.discard(mGoogleApiClient);
            }
        };

For now I am relying on this code. I have been able to open Google Sheets and Docs but I need to upload them as well as images and videos to my app server (Opening the file is not necessary). Is there a way I can achieve this?

I have read about the Storage Access Framework (SAF) and it may allow me to access the Google Drive files Kitkat onward but my app supports devices running on JellyBean and upwards and thus I am going with the GGDA.

I have not checked the REST API yet. I want to understand if I am doing this the right way or is there a totally different way to achieve what I want. And if this is the right way, what is going wrong?

Any help will be appreciated.

You may try the following workaround:

  • Make sure that you generate the correct configuration with your correct sha1.
  • Make sure that the google-services.json in your app folder is correct.
  • Try to go to Credentials page and make sure that you choose the correct project. It should be same as the one you used to generate configuration files.
  • In the Credentials section , make sure that your OAuth 2.0 client IDs type is Web Application not Android.
  • Make sure that your API key in your string.xml matches the one in your Google Developer Console.

If these don't help, try to create a new API key based on a new project in Google Developer Console.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM