Google drive api getToken()只提供刷新标记

Google drive api getToken()只提供刷新标记,google-drive-api,Google Drive Api,我正在开发一个Android应用程序,该应用程序登录到Gdrive服务,在请求用户授予权限后,它保存令牌并将其发送到我们的后端,即请求Gdrive服务本身获取内容的后端。以前,我使用AccountManager登录,但几周前,我收到了一封来自谷歌的电子邮件,要求将授权过程转移到谷歌play服务库。我在获取令牌时遇到问题,以下是我的代码: GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(myApp.g

我正在开发一个Android应用程序,该应用程序登录到Gdrive服务,在请求用户授予权限后,它保存令牌并将其发送到我们的后端,即请求Gdrive服务本身获取内容的后端。以前,我使用AccountManager登录,但几周前,我收到了一封来自谷歌的电子邮件,要求将授权过程转移到谷歌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); 
            }
Account=mAccounts[位置];
凭证.setSelectedAccountName(account.name);
新建GetDriveServiceTask().execute(凭证);
私有类GetDriveServiceTask扩展了AsyncTask{
受保护的Void doInBackground(GoogleAccountCredential…凭据){
试一试{
GoogleAccountCredential凭证=凭证[0];
String token=credential.getToken();
}捕获(UserRecoverableAuthe异常){
UserRecoverableAuthException=(UserRecoverableAuthException)e;
Intent authorizationIntent=exception.getIntent();
startActivityForResult(授权意图、请求权限);
}
当用户之前授予访问他/她的帐户的权限时,我遇到了一个问题,命令credential.getToken();只给了我我认为是刷新令牌的内容,类似于:“1/SGvUDUNnRG8wVs_reiskx1xUupWakeEq5FHXH-rbOk”…我如何从中获取访问令牌

我试着对url进行Post调用“https://accounts.google.com/o/oauth2/token“如本文所述:但我每次都会得到未经授权的客户

请告诉我,我被困在这里,邮件上说,AccountManager方法将于2月1日停止工作


提前感谢!

您应该授权使用Google Play服务,这只会给您一个访问令牌。 有关如何使用Google Play服务授权Android应用程序以向驱动器API发送请求的示例,请参阅以下快速入门指南:


我遇到了完全相同的问题。这不是所问问题的答案!我已经搜索了好几天的解决方案,我所找到的都是来自谷歌工程师的不屑一顾的答案。