android - G+ 登录 Ian Barber 实现 : onConnectionFailed not getting called when disconnecting rebuilding GoogleApiClient

标签 android google-plus google-plus-signin

我已经使用开发者网站集成了 google+ 登录,对于服务器端,我遵循了 riskcompletefailure 博客上@ianbarber 的解决方案

这就是我构建 GoogleApiClient 的方式:

return new GoogleApiClient.Builder(getActivity())
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API, Plus.PlusOptions.builder().build())
            .addScope(new Scope("email")).addScope(Plus.SCOPE_PLUS_LOGIN)
            .build();

这是在异步任务中获取 id token 的方法

scopes = "audience:server:client_id:" + Util.CLIENT_ID_SERVER;
            id = GoogleAuthUtil.getToken(getActivity(),
                    Plus.AccountApi.getAccountName(mGoogleApiClient),
                    scopes);

之后调用 createSession 并根据服务器的响应更新 hashMap 并重建 GoogleApiClient 并再次重试连接,但它将转到 onConnceted() 而不是 onConnectionFailed() 因为服务器返回 401(没有可用的刷新 token ) )

if (cookie.equals("NO_REFRESH")) {
        Log.v(TAG, "create session for " + mAccountName);
        mSessionCookie = null;
        mUserHasRefreshOnServer.put(mAccountName, false);
    } else {
        Log.v(TAG, "refresh available on server");
        mSessionCookie = cookie;
        mUserHasRefreshOnServer.put(mAccountName, true);
    }
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
        Log.v(TAG, "disconnect googleclient ");
    }
    Log.v(TAG, "try connect googleclient ");

    mGoogleApiClient = buildGoogleApiClient();
    mGoogleApiClient.connect();
}

这是我需要在 onConnectionFailed() 中执行的操作

public void onConnectionFailed(ConnectionResult result) {

    if ((mAccountName != null)
            && mUserHasRefreshOnServer.containsKey(mAccountName)
            && mUserHasRefreshOnServer.get(mAccountName) == Boolean.FALSE) {
            getCode(); //code for getting onetime authorization code
    }

请指出我做错了什么,有关详细信息,请查看 document我完全按照了

编辑:

我改变了流程..完全复制这个 gist但是现在在发送 id token 后(假设服务器没有刷新并且没有征得同意)当我去获取代码时 我只获得了对 Plus.SCOPE_PLUS_LOGIN 的同意,而不是对离线访问的同意,因此当我重新连接时,我仍然在 onConnectionFailed 中结束,错误代码 = 4(需要登录)

但是, 备用流程,其中如果我事先解决错误(同意 Plus.SCOPE_PLUS_LOGIN),然后这次去获取 getCode,我也获得了此离线访问同意,因此需要一个 onConnected。

最佳答案

这是进入的有效状态!如果用户之前已登录,您可以到达那里,但您丢失了服务器上的刷新 token 。

如果您查看要点(当然,自从迁移到 GoogleApiClient 以来,它已经过时了),您可以看到我们在 onConnected 中也有一个 getCode 调用:https://gist.github.com/ianbarber/7105273#file-codeactivity-java-L97

if (mSessionCookie == null) {
  // If the server hasn't got a refresh token, grab it.
  if(mUserHasRefreshOnServer.containsKey(mAccountName)) {
    getCode();
  } else {
    // Otherwise, just get the session.
    getSession();
  }
}

这正是为了处理这种情况——我们知道用户已经签名,但是我们仍然需要在服务器上获得一个刷新 token ,所以我们必须将它们发送到 getCode 路由并弹出一个同意通过 UserRecoverableAuthException 进行对话。

关于android - G+ 登录 Ian Barber 实现 : onConnectionFailed not getting called when disconnecting rebuilding GoogleApiClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28692432/

相关文章:

java - 如何解析 cookie 字符串

android - 将特定表解析为字符串数组 Jsoup

java - 如何在android中的 ListView 中获取项目值?

来自 Google plus 的 URL 共享

ios - 将 cocoapod 依赖项添加到 cocoa touch 框架

android - Plus.PeopleApi.getCurrentPerson 在 Play 服务 8.4 中已弃用。如何使用 GoogleSignInApi 获取用户的名字、姓氏和性别?

android - 添加proguard时出错

c# - 发布后谷歌登录不起作用

ios - 无法获得 https ://www. googleapis.com/auth/plus.login 范围权限的授权成功

android - 上传到 Google Play 时 Google Plus 登录失败