使GoogleAuthUtil停止要求用户脱机访问



我正在尝试为我的后端开发一个具有客户端Google oAuth2.0授权的应用程序。我已经在谷歌开发者控制台上完成了所有需要的工作。每当应用程序请求使用GoogleAuthUtil.getToken()的授权令牌时,谷歌就会不断要求离线访问。我想一劳永逸地给予许可,这样授权过程就变得沉默了。

这是我在授权AsyncTask中使用的代码。

protected String doInBackground(String... arg0) {
    String token = null;
    try {
        token = GoogleAuthUtil.getToken(mainActivity, mEmail, "oauth2:server:client_id:"+clientId+":api_scope:https://www.googleapis.com/auth/plus.login");
    } catch (GooglePlayServicesAvailabilityException playEx) {
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(
                playEx.getConnectionStatusCode(),
                mainActivity,
                1001);
        dialog.show();
    } catch (UserRecoverableAuthException recoverableException) {
        Intent recoveryIntent = recoverableException.getIntent();
        mainActivity.startActivityForResult(recoveryIntent,1001);
        // Use the intent in a custom dialog or just startActivityForResult.
        Log.e("Auth", "Recoverable authentication exception: " + recoverableException.getMessage(), recoverableException);
    } catch (GoogleAuthException authEx) {
        // This is likely unrecoverable.
        Log.e("Auth", "Unrecoverable authentication exception: " + authEx.getMessage(), authEx);
    } catch (IOException ioEx) {
        Log.i("Auth", "transient error encountered: " + ioEx.getMessage());
    }
    return token;
}

好吧,所以我终于找到了一些可以帮助你们中仍在努力解决这个问题的东西。

我把范围改成这样:

"audience:server:client_id:" + clientId

就是这样。即使没有指定"plus.login"范围,它也能工作。

希望这能有所帮助!

最新更新