小编Rub*_*Kid的帖子

getToken() 只提供刷新令牌

我正在开发一个登录 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 …

google-drive-api

5
推荐指数
1
解决办法
1397
查看次数

标签 统计

google-drive-api ×1