我正在开发一个登录 Gdrive 服务的 Android 应用程序,在要求用户授予权限后,它保存令牌并将其发送到我们的后端,即向 gdrive 服务本身询问内容的后端。之前,我使用 AccountManager 登录,但几周前我收到一封来自 Google 的电子邮件,要求将授权流程移至 Google Play 服务库。我在获取令牌时遇到问题,这是我的代码:
GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(myApp.getApplicationContext(), DriveScopes.DRIVE);
Account[] mAccounts = credential.getAllAccounts();
Run Code Online (Sandbox Code Playgroud)
选择要登录的账号后,监听器是这样的:
Account account = mAccounts[position];
credential.setSelectedAccountName(account.name);
new GetDriveServiceTask().execute(credential);
private class GetDriveServiceTask extends AsyncTask<GoogleAccountCredential, Void, Void> {
protected Void doInBackground(GoogleAccountCredential... credentials) {
try{
GoogleAccountCredential credential = credentials[0];
String token = credential.getToken();
} catch (UserRecoverableAuthException e) {
UserRecoverableAuthException exception = (UserRecoverableAuthException) e;
Intent authorizationIntent = exception.getIntent();
startActivityForResult(authorizationIntent,REQUEST_PERMISSIONS);
}
Run Code Online (Sandbox Code Playgroud)
当用户之前允许访问他/她的帐户时,我遇到了问题,命令 credential.getToken(); 只给了我我认为是刷新令牌的东西,例如:“1/SGvUDUNnRG8wVs_REISKx1xuupbwAKEEQ5fHXH-rbOk”……我如何从中获取访问令牌?
我尝试按照此处所述对网址“https://accounts.google.com/o/oauth2/token”进行 Post 调用:https : //developers.google.com/accounts/docs/OAuth2WebServer#refresh但我是每次都获得未授权的客户端...
请我被困在这里,电子邮件说,AccountManager 方法将在 2 月 1 日停止工作......
提前致谢!
Cla*_*ino -2
您应该授权使用 Google Play 服务,这只会给您一个访问令牌。有关如何使用 Google Play 服务授权您的 Android 应用程序以便向 Drive API 发送请求的示例,请查看以下快速入门指南:
https://developers.google.com/drive/quickstart-android
| 归档时间: |
|
| 查看次数: |
1397 次 |
| 最近记录: |