在服务器上下载 DriveFIle

Download DriveFIle on server

我有一个 Android 应用程序,我的用户可以在其中 select 上传文档,我想在我的服务器上传递可用于下载文件的 URL。我的大部分代码都基于此:

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

我也在使用这些指南来下载文件:

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

我面临的问题是 getWebContentLink 方法每次都返回 null。我无法在我的服务器上下载该文件(即使使用 OAuth 2.0 客户端 ID)。我在这里遗漏了什么吗?

我可以 getAlternateLink URL 但这只是为了在浏览器上查看文档,这不是我想要的。下面是我的一些相关方法:

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();
    }
}

我的目标是让我的用户 select 应用程序上的文件,然后让服务器下载该文件。

我可以这样做:

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

获得令牌后,您可以按照此页面提出请求:

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

Authorizationheader的代币在上面,resourceId你可以从DriveId获得。