List files from Google Drive and get downloadUrl for selected file in Android

[亡魂溺海] 提交于 2019-12-04 15:24:23
seanpj

In order to use the file/folder outside of your GDAA based app, you need so-called ResourceID. This ResourceId is a string that uniquely identifies the object of Google Drive (see here)

Turn DriveId into ResourceId:

        DriveId driveId = metadata.getDriveId();
        String resourceId = driveId.getResourceId();

Once you have ResourceId, you can construct 'download URL' for your server app from it. The ResourceID string is the one found if you go to drive.google.com, select a file/folder and perform rightbutton > getLink.
(looks like 'https://drive.google.com/open?id=0B1mQUW2__I_am_the_resource_id')

Just take this string '0B1mQUW2__I_am_the_resource_id' to the 'TryIt' playground here and paste it to the 'fileId' field.

So, the short answer is that you don't need RESTful Api to get the file/folder identifier you can use elsewhere.

Second part of your question, 'How do I handle the folder hierarchy if I have to build my UI to show these files?' is answered (somewhat) in the 'createTree()/testTree()' methods of MainActivity of these 2 demos (GDAADemo, RESTDemo). These are the same tasks implemented on the GDAA as well as the REST Apis and the choice depends mainly on the SCOPE your app needs (GDAA supports only the FILE scope, whereas REST supports both the FILE and the DRIVE scopes)

Good Luck

The Activity:

public class GoogleDriveActivity extends Activity implements
GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks {

private GoogleApiClient mGoogleApiClient;
final HttpTransport transport = AndroidHttp.newCompatibleTransport();
final JsonFactory jsonFactory = GsonFactory.getDefaultInstance();

public static final int REQUEST_CODE_RESOLUTION = 1;
public static final int REQUEST_CODE_SELECT = 2;

private static final String[] SCOPES = { DriveScopes.DRIVE_FILE};
private static final String TAG = "GoogleDrive";
private String accountName;


@Override
protected void onResume() {
    super.onResume();
    setupGoogleClient();
}


@Override
protected void onPause() {
    super.onPause();
    if (mGoogleApiClient != null) {
        mGoogleApiClient.disconnect();
    }
}

@Override
public void onConnected(Bundle bundle) {
    IntentSender intentSender = Drive.DriveApi
            .newOpenFileActivityBuilder()
            .build(mGoogleApiClient);

    AccountManager manager = (AccountManager) getSystemService(ACCOUNT_SERVICE);
    Account[] list = manager.getAccountsByType("com.google");
    //Getting the first account because that is the primary account for that user
    accountName = list[0].name;

    try {
        startIntentSenderForResult(intentSender, REQUEST_CODE_SELECT, null, 0, 0, 0);
    } catch (IntentSender.SendIntentException e) {
        Log.w(TAG, "Unable to send intent to connect to Google API client " + e.getMessage());
    }

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    if (!connectionResult.hasResolution()) {
        return;
    }
    try {
        connectionResult.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
    } catch (IntentSender.SendIntentException e) {
        Log.w(TAG, "Unable to send intent to connect to Google API client " + e.getMessage());
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case REQUEST_CODE_SELECT:
            if (resultCode == RESULT_OK) {
                DriveId driveId = data.getParcelableExtra(OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);
                String resourceId = driveId.getResourceId();
                new GoogleDriveAsync(this).execute(resourceId);
            }
            finish();
            break;
        case REQUEST_CODE_RESOLUTION:
            if (resultCode == RESULT_OK) {
                mGoogleApiClient.connect();
            }
            break;
        default:
            super.onActivityResult(requestCode, resultCode, data);
            break;
    }
}

@Override
public void onConnectionSuspended(int i) {
    Log.w(TAG, "Connection to Google API client suspended");
}

private void setupGoogleClient() {
    if (mGoogleApiClient == null) {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
    }
    mGoogleApiClient.connect();
}



class GoogleDriveAsync extends AsyncTask<String, Void, Void> {
    private GoogleDriveActivity activity;

    GoogleDriveAsync(GoogleDriveActivity activity) {
        this.activity = activity;
    }

    @Override
    protected Void doInBackground(String... args) {
        try {
            String id = args[0];
            GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(getApplicationContext(), Arrays.asList(SCOPES));
            credential.setBackOff(new ExponentialBackOff());
            credential.setSelectedAccountName(accountName);
            com.google.api.services.drive.Drive service = new com.google.api.services.drive.Drive.Builder(transport, jsonFactory, credential).build();
            File file = service.files().get(id).setFields("downloadUrl").execute();
            if (file != null) {
                String strUrl = file.getDownloadUrl();
                String token = GoogleAuthUtil.getToken(activity, accountName, "oauth2: " + Drive.SCOPE_FILE);
//This is your downloadUrl and token
                Log.w(TAG, "download link is " + strUrl + " and token is " + token);
            }
        } catch (final GooglePlayServicesAvailabilityIOException availabilityException) {
            Log.w(TAG, "Google Play Services not available to get downloadUrl of selected file");
        } catch (UserRecoverableAuthIOException userRecoverableException) {
            Log.w(TAG, "User authorization error in getting downloadUrl " + userRecoverableException.getMessage());
        } catch (Exception e) {
            Log.w(TAG, "Exception in getting downloadUrl " + e.getMessage());
        }
        return null;
    }
}
}

The AndroidManifest (relevant lines) :

<uses-permission android:name="android.permission.GET_ACCOUNTS" />

<activity
            android:name="com.myapp.GoogleDriveActivity"
            android:label="@string/app_name"
            <meta-data android:name="com.google.android.apps.drive.APP_ID" android:value="id=<your-gogole-project-id>"/>
</activity>

The build.gradle (relevant lines):

compile 'com.google.android.gms:play-services-drive:7.8.0'
compile 'com.google.api-client:google-api-client:1.20.0'
compile 'com.google.api-client:google-api-client-android:1.20.0'
compile 'com.google.api-client:google-api-client-gson:1.20.0'
compile 'com.google.apis:google-api-services-drive:v2-rev170-1.20.0'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!