Android谷歌无法获得授权代码

前端之家收集整理的这篇文章主要介绍了Android谷歌无法获得授权代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试根据这篇文章获得Google身份验证代码Server-side access for your app.但是,每当我运行我的应用程序并尝试获取代码时,我在LogCat中获得以下内容
07-23 23:42:31.760: E/getAccessToken()(11114): [ERROR] GoogleAuthException: com.google.android.gms.auth.GoogleAuthException: Unknown

我浏览了很多东西,据我所知,我已经在下面的代码中正确设置了所有内容.任何想法为什么它没有获得身份验证令牌?

仅供参考,我在API控制台中有一个项目设置,其中包含为oauth配置的应用程序和Web应用程序,而我正在使用作用域字符串的服务器(而不是应用程序)ID.我也跟着上面链接中的示例,应用程序工作正常,允许您登录/退出,但我无法获得身份验证代码.

private String getAccessToken() {
    String scopesString = Scopes.PLUS_LOGIN + " " + Scopes.PLUS_PROFILE;
    String scope = "oauth2:server:client_id:" + SERVER_CLIENT_ID + ":api_scope:" + scopesString;
    Bundle appActivities = new Bundle();
    appActivities.putString(GoogleAuthUtil.KEY_REQUEST_VISIBLE_ACTIVITIES,"http://schemas.google.com/AddActivity");
    String code = null;
    try {
         code = GoogleAuthUtil.getToken(
                    this,// Context context
                    mPlusClient.getAccountName(),// String accountName
                    scopes,// String scope
                    appActivities                     // Bundle bundle
                );
    } catch (IOException transientEx) {
      // network or server error,the call is expected to succeed if you try again later.
      // Don't attempt to call again immediately - the request is likely to
      // fail,you'll hit quotas or back-off.
        Log.e("getAccessToken()","[ERROR] IOException: " + transientEx);
        return null;
    } catch (UserRecoverableAuthException e) {
           // Recover
        Log.e("getAccessToken()","[ERROR] UserRecoverableAuthException: " + e);
        code = null;
    } catch (GoogleAuthException authEx) {
      // Failure. The call is not expected to ever succeed so it should not be
      // retried.
        Log.e("getAccessToken()","[ERROR] GoogleAuthException: " + authEx);
      return null;
    } catch (Exception e) {
        Log.e("getAccessToken()","[ERROR] Exception: " + e);
      throw new RuntimeException(e);
    }
    return code;
}

解决方法

我不知道你是否修改过该行以发布问题,但查看你发布的代码,这一行是错误的:
String scopes = "oauth2:server:client_id:<My server client ID>:scopesString";

它应该是:

String scopes = "oauth2:server:client_id:" + SERVER_CLIENT_ID + ":api_scope:" + scopeString;
原文链接:https://www.f2er.com/android/315244.html

猜你在找的Android相关文章