【问题标题】:Google play service authentication (oauth 2.0)谷歌播放服务认证(oauth 2.0)
【发布时间】:2013-08-16 15:41:04
【问题描述】:

我希望使用 oauth2 协议对我的 android 应用程序的用户进行身份验证,以便用户无需为我的应用程序创建登录名和密码。

为此,我遵循以下教程:http://android-developers.blogspot.fr/2013/01/verifying-back-end-calls-from-android.html

我已经导入了 google play service sdk 示例(在 AndroidSDK\extras\google\google_play_services\samples\auth 中),并且我已经更改了范围值。

但是,我无法获得身份验证令牌...虽然我已经正确配置了我的 google 控制台 API。

这里是调用getToken方法的函数:

  protected String fetchToken() throws IOException {
      try {
          return GoogleAuthUtil.getToken(mActivity, mEmail, mScope);
      } catch (GooglePlayServicesAvailabilityException playEx) {
          // GooglePlayServices.apk is either old, disabled, or not present.
          mActivity.showErrorDialog(playEx.getConnectionStatusCode());
      } catch (UserRecoverableAuthException userRecoverableException) {
          // Unable to authenticate, but the user can fix this.
          // Forward the user to the appropriate activity.
          mActivity.startActivityForResult(userRecoverableException.getIntent(), mRequestCode);
      } catch (GoogleAuthException fatalException) {
          onError("Unrecoverable error " + fatalException.getMessage(), fatalException);
      } 
      return null;
  }

这是我的范围:audience:server:client_id:MY_CLIENT_ID.apps.googleusercontent.com

当我尝试这样做时,我得到以下异常:

08-16 17:22:08.471: E/TokenInfoTask(16546): Exception: 
08-16 17:22:08.471: E/TokenInfoTask(16546): com.google.android.gms.auth.GoogleAuthException: Unknown
08-16 17:22:08.471: E/TokenInfoTask(16546):     at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)

【问题讨论】:

    标签: android google-api google-oauth google-play-services google-authentication


    【解决方案1】:

    您应该使用 Google 于 2 月发布的 Google+ 登录,因为这将使您获得跨客户端授权并访问更多可用于个性化您的应用的数据。在幕后,此授权库使用 OAuth 2 连接到 Google 服务。

    一些帮助您入门的资源:

    【讨论】:

    • 感谢您的建议,但使用 Google+ 登录,用户必须点击一个按钮,而使用 google play 服务身份验证则不需要...
    • 从什么时候开始,GoogleAuthUtil.getToken() 方法开始为未在设备上注册的电子邮件抛出带有消息“BadUsername”的 GoogleAuthException,而不是之前带有消息“Non existing account”的异常 IllegalArgumentException '电子邮件地址'” ? developer.android.com/reference/com/google/android/gms/auth/…, java.lang.String, java.lang.String)
    • 请求 userinfo.profile 和 plus.login 会导致这个异常。请从您的请求中删除 userinfo.profile 范围。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-18
    • 2014-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-28
    • 2014-11-20
    相关资源
    最近更新 更多