KVISH
KVISH

Reputation: 13208

Download DriveFIle on server

I have an Android application where my user can select a document to upload and on my server I want to pass the URL that can be used to download the file. I'm basing much of my code on this:

http://www.101apps.co.za/index.php/articles/android-apps-and-google-drive-picking-files.html

and I'm also using these guidelines to download the file:

https://developers.google.com/drive/web/manage-downloads

Problem i'm facing is that the getWebContentLink method is returning null every time. I'm not able to download the file ever on my server (even with the OAuth 2.0 client ID). Is there something i'm missing here?

I'm able to getAlternateLink URL but that is only for viewing the document on a browser, which is not what I want. I have some of my relevant methods below:

public GoogleApiClient getGoogleApiClient() {
    if (mGoogleApiClient == null) {
        mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
    }
    return mGoogleApiClient;
}

// Google Drive call back
public void onConnected(Bundle bundle) {
    if (getGoogleApiClient().isConnected()) {
        getDataFromGoogleDrive();
    }
}

public void onConnectionSuspended(int i) {
}

public void onConnectionFailed(ConnectionResult connectionResult) {
    if (connectionResult.hasResolution()) {
        try {
            connectionResult.startResolutionForResult(getActivity(), GDRIVE_CONNECTION_REQUEST);
        } catch (Exception e) {
            AlertUtil.showOkAlert(getActivity(), "Oops", "We were unable to connect to your Google Drive.");
        }
    } else {
        GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(), getActivity(), 0).show();
    }
}

private void getDataFromGoogleDrive() {
    try {
        IntentSender intentSender = Drive.DriveApi.newOpenFileActivityBuilder().build(getGoogleApiClient());
        getActivity().startIntentSenderForResult(intentSender, GDRIVE_FILE_REQUEST, null, 0, 0, 0);
    } catch (Exception e) {
        Log.w(TAG, "ERROR connecting!", e);
        getGoogleApiClient().connect();
    }
}

My goal is to allow my user to select a file on the app and then have the server download the file.

Upvotes: 1

Views: 79

Answers (1)

KVISH
KVISH

Reputation: 13208

I was able to do it this way:

// accountName is the email address that was chosen
token = GoogleAuthUtil.getTokenWithNotification(fragment.getActivity(), accountName, scope, null);

Once you get a token you can make requests as per this page:

https://developers.google.com/drive/web/manage-downloads

The token for Authorization header is above and the resourceId you can get from DriveId.

Upvotes: 1

Related Questions