我使用Google Drive Android API让用户将Android设备上的Google云端硬盘文件上传到我的服务器。
我现在想要更改此设置,以允许我的服务器使用跨客户端授权直接访问用户Google云端硬盘,这意味着我必须使用ID Token
生成GoogleAuthUtil.getToken
。这就是一切都没有了。为了生成此令牌,我需要一个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
。
-
是否存在我从根本上误解的事情,或者这是否真的应该如何运作?
答案 0 :(得分:3)
您不应再使用GoogleAuthUtil.getToken()
,如this blog post中所述,因为将这些令牌传递到您的服务器是不安全的。
相反,您应该通过Enable Server-Side Access使用Google Sign In并使用requestServerAuthCode() method。
这样可以避免双重提示用户,不使用GET_ACCOUNTS
权限,并为您提供单个用户流登录。