Google Drive android的保存和恢复令牌

Google Drive android的保存和恢复令牌,android,google-drive-api,Android,Google Drive Api,我试着从谷歌的例子,以验证谷歌驱动器,它可以工作。但问题是我不知道如何从用户那里恢复以前的成功登录。例如,当用户成功登录时,下次转到我的应用程序时,他们不需要再次登录。我查看了GoogleAccountCredential类,它只有getToken方法,没有'setToken',所以我不知道该怎么做。文件上也没有说什么。以下是我的尝试: credential = GoogleAccountCredential.usingOAuth2(context, DriveScopes.DRIVE);

我试着从谷歌的例子,以验证谷歌驱动器,它可以工作。但问题是我不知道如何从用户那里恢复以前的成功登录。例如,当用户成功登录时,下次转到我的应用程序时,他们不需要再次登录。我查看了GoogleAccountCredential类,它只有getToken方法,没有'setToken',所以我不知道该怎么做。文件上也没有说什么。以下是我的尝试:

   credential = GoogleAccountCredential.usingOAuth2(context, DriveScopes.DRIVE);

   // try to add login account into credential
   String accountName = SharePreferenceHelper.getDriveAccount(context);
    if (accountName != null) {
    credential.setSelectedAccountName(accountName);
    service = getDriveService(credential);
    }

     // try to get token again
   try {
    String token = credential.getToken();   
        Log.d(TAG,"token = " + token);
    } catch (UserRecoverableAuthException ex) {
                 startActivityForResult(ex.getIntent(), requestCode);

    } 

有人知道怎么做吗?

您不需要手动操作。它已经由GoogleAccountCredential处理

这个电话

credential=GoogleAccountCredential.UsingAuth2此为作用域

将自动还原已保存的令牌(如果以前已保存)

您可以在此处查看我的应用程序的示例源代码:


区别在于它使用Google Analytics而不是Google Drive。

谢谢,事实上是我做的,但它不起作用。我发现我的问题是使用错误的上下文初始化GoogleAccountCredential,我将其投入服务,因此我必须使用getApplicationContext来初始化凭据,如下所示: