首页 > 解决方案 > 哪些是在我的 Android 应用程序中从 Google Drive 下载 google 表格所需的服务器端权限集

问题描述

我的具体问题是

当我打电话时

driveService.files().export(fileId, "text/csv").executeMediaAndDownloadTo(outputStream);

从我的应用程序我得到

11-22 00:35:02.489 : com.google.api.client.http.HttpResponseException: 401 Unauthorized
11-22 00:35:02.490 : {
11-22 00:35:02.491 :  "error": {
11-22 00:35:02.491 :   "errors": [
11-22 00:35:02.491 :    {
11-22 00:35:02.491 :     "domain": "global",
11-22 00:35:02.491 :     "reason": "authError",
11-22 00:35:02.491 :     "message": "Invalid Credentials",
11-22 00:35:02.492 :     "locationType": "header",
11-22 00:35:02.492 :     "location": "Authorization"
11-22 00:35:02.492 :    }
11-22 00:35:02.492 :   ],
11-22 00:35:02.492 :   "code": 401,
11-22 00:35:02.492 :   "message": "Invalid Credentials"
11-22 00:35:02.493 :  }
11-22 00:35:02.493 : }

当我在 Services / Drive API v3 / drive.files.export 上使用 APIs Explorer 时,我得到了我想要的数据。

我的问题是:

  1. 我该如何解决这个问题。

  2. 当我登录时获得了一个谷歌帐户......这不足以知道我输入的用户/密码以及在智能手机中注册的用户/密码......我是对的吗?

  3. 哪些是我需要在服务器端设置的最小权限集才能获得权限

  4. 如何检查哪些是不允许从谷歌驱动器下载谷歌表的特定权限问题?

非常感谢您的任何回复。

我与此问题相关的 Java 代码片段(登录过程)

在服务器端

我的应用程序凭据是:

我使用“Web 客户端”的客户端 ID 如果我使用 Android 客户端的 clientId,我不会得到 idToken

启用库 API:

在安卓应用中

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(this.googleClientId)
            .requestEmail()
            .build();

    ...

    mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

    ...
    Intent signInIntent = mGoogleSignInClient.getSignInIntent();
    startActivityForResult(signInIntent, RC_SIGN_IN);
    ...

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RC_SIGN_IN) {
        validateGoogleUser(data);
    }
}

...
    Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(this.dataIntent);
...
    GoogleSignInAccount account = task.getResult(ApiException.class);
...
    // It returns my account with id / idToken / mail / names / ... more


    And then... I continue

在我尝试下载的方法中,这是代码

        //TODO: move this to a service and to a dao
        final HttpTransport HTTP_TRANSPORT  = AndroidHttp.newCompatibleTransport();;
        GoogleCredential credential = new GoogleCredential().setAccessToken(accessToken);



        Drive driveService = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
                .setApplicationName(applicationName)
                .build();
        String fileId = idSheet;
        //TODO: Check how to check progress and limit size of download or cancel the operation after n seconds
        OutputStream outputStream = new ByteArrayOutputStream();
        driveService.files().export(fileId, "text/csv")
                .executeMediaAndDownloadTo(outputStream);

注意:我不使用 - GoogleIdTokenVerifier.verify

我也不知道它是什么......因为我已经得到了我的帐户信息。

标签: androidoauth-2.0google-apigoogle-drive-apigoogle-signin

解决方案


我找到了问题的解决方案。服务器端的权限没问题。但在这个项目中,我分叉的是谷歌登录和谷歌驱动器连接的组合,我需要从我的 Android 应用程序下载或使用谷歌驱动器文件

https://github.com/jguastav/android-GoogleDriveSample


推荐阅读