我正在开发一个登录 Gdrive 服务的 Android 应用程序,在要求用户授予权限后,它会保存令牌并将其发送到我们的后端,即向 gdrive 服务本身询问内容的后端。之前,我使用 AccountManager 登录,但几周前我收到了一封来自 Google 的电子邮件,要求将授权流程移至 Google Play 服务库。我在获取令牌时遇到问题,这是我的代码:
GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(myApp.getApplicationContext(), DriveScopes.DRIVE);
Account[] mAccounts = credential.getAllAccounts();
选择要登录的帐户后,侦听器会执行以下操作:
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);
}
当用户之前访问他/她的帐户时,我遇到了问题,命令 credential.getToken(); 只给我我认为是刷新令牌的东西,例如:“1/SGvUDUNnRG8wVs_REISKx1xuupbwAKeEQ5fHXH-rbOk”...我怎样才能从中获取访问令牌?
我尝试对 URL“https://accounts.google.com/o/oauth2/token”进行 Post 调用,如下所述:https ://developers.google.com/accounts/docs/OAuth2WebServer#refresh但我是每次都获得未授权客户...
请我被困在这里,电子邮件说,AccountManager 方法将在 2 月 1 日停止工作......
提前致谢!