【问题标题】:Google Drive Android API and Cross Client AuthorizationGoogle Drive Android API 和跨客户端授权
【发布时间】:2016-06-29 20:00:37
【问题描述】:

我使用 Google Drive Android API 让用户将文件从他们 Android 设备上的 Google Drive 上传到我的服务器。 我现在想更改此设置以允许我的服务器使用跨客户端授权直接访问用户 Google Drive,这意味着我必须使用GoogleAuthUtil.getToken 生成ID Token。这就是一切都出轨了。为了生成这个令牌,我需要一个 Account 对象。

目前,我正在像这样设置GoogleApiClient

mGoogleApiClient = new GoogleApiClient.Builder(getContext())
    .addApi(Drive.API)
    .addScope(Drive.SCOPE_FILE)
    .addConnectionCallbacks(this)
    .addOnConnectionFailedListener(this)
    .build();

这很好用,它时不时地显示一个帐户选择器,用户可以在其中选择要连接的帐户。全部由框架管理。但是无法访问用户选择连接的Account

相反,我必须自己管理它,使用:

Intent intent = AccountPicker.newChooseAccountIntent(null, null, new String[]{"com.google"},
    false, null, null, null, null);
startActivityForResult(intent, RC_ACCOUNT_PICKER);

onActivityResult 中,我可以使用:

Account account = new Account(intent.getExtras().getString(AccountManager.KEY_ACCOUNT_NAME),
    intent.getExtras().getString(AccountManager.KEY_ACCOUNT_TYPE));

现在,由于我有一个 Account 对象,我可以使用它显式连接:

mGoogleApiClient = new GoogleApiClient.Builder(getContext())
    .addApi(Drive.API)
    .addScope(Drive.SCOPE_FILE)
    .setAccountName(mAccount.name) // Account name!
    .addConnectionCallbacks(this)
    .addOnConnectionFailedListener(this)
    .build();

这是我唯一的选择吗,真的没有办法让用户使用我的第一个解决方案选择Account

--

稍后,当尝试使用以下方法生成我的ID Token 时:

String scope = String.format("oauth2:server:client_id:%s:api_scope:%s",
    "12345.apps.googleusercontent.com", 
    "https://www.googleapis.com/auth/drive.file");
String token = GoogleAuthUtil.getToken(getContext(), mAccount, scope);

我仍然得到一个UserRecoverableAuthException,我需要从用户那里获得另一个权限,即使我使用与连接时相同的帐户和相同的身份验证范围。 处理完所有这些后,我可以再次运行相同的代码并最终获得我的ID Token

--

我是否从根本上误解了某些东西,或者这真的是它应该如何工作的?

【问题讨论】:

    标签: android google-api google-drive-api google-drive-android-api android-googleapiclient


    【解决方案1】:

    您不应再使用GoogleAuthUtil.getToken(),如this blog post 中所述,因为将这些令牌传递到您的服务器是不安全的。

    相反,您应该通过Google Sign In 使用Enable Server-Side Access 并使用requestServerAuthCode() method

    这避免了双重提示用户,不使用GET_ACCOUNTS权限,并为您提供单一的用户登录流程。

    【讨论】:

    • 我可以问一个问题吗?在博客上,In general, you should NOT use GoogleAuthUtil.getToken, unless you are making REST API call on Android client.,如果我理解正确,这意味着如果直接在 Android 应用程序(没有任何服务器)上调用对 REST API 的请求,仍然可以安全地使用GoogleAuthUtil.getToken
    • 如果您想在本地使用 id 令牌,您可能仍想使用 id token flow 并使用 requestIdToken() 来获得单点登录的优势。
    • 我明白了,在这种情况下,GoogleAuthUtil.getToken 仍然是安全的吗?此外,id token 仅用于获取user's ID, email address, name, or profile picture URL,不用于访问其他 Google API,例如 Google Drive、Youtube 或 Contacts
    • 谢谢,太好了!有什么方法可以在不使用登录 API 的情况下请求身份验证代码,这没什么大不了的,但我不需要了解用户的基本信息和个人资料,我只希望他们通过我的应用访问他们的云端硬盘。
    • 如果您使用空白的GoogleSignInOptions.Builder(即使用new GoogleSignInOptions.Buidler() 构造函数),那么您将无法获得基本信息和配置文件(您需要使用GoogleSignInOptions.DEFAULT_SIGN_IN 或专门调用@ 987654336@ 和 requestProfile() 这样做)
    猜你喜欢
    • 2018-07-11
    • 2023-03-24
    • 2015-12-26
    • 1970-01-01
    • 2020-09-23
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多