Android 谷歌登录请求年龄不工作

Android 谷歌登录请求年龄不工作,android,oauth,token,google-signin,Android,Oauth,Token,Google Signin,我尝试用Android登录谷歌,然后我将令牌发送到我的服务器,并尝试访问数据 我想访问用户的年龄范围,但似乎无法访问 以下是Android代码: // Configure sign-in to request the user's ID, email address, and basic // profile. ID and basic profile are included in DEFAULT_SIGN_IN. GoogleSignInOptions gso = n

我尝试用Android登录谷歌,然后我将令牌发送到我的服务器,并尝试访问数据

我想访问用户的年龄范围,但似乎无法访问

以下是Android代码:

    // Configure sign-in to request the user's ID, email address, and basic
    // profile. ID and basic profile are included in DEFAULT_SIGN_IN.
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .requestProfile()
            .requestId()
            .requestIdToken(mView.getTarget().getString(R.string.google_server_client_id))
            .requestScopes(new Scope("https://www.googleapis.com/auth/profile.agerange.read"))
            .build();

    // Build a GoogleApiClient with access to the Google Sign-In API and the
// options specified by gso.
    mGoogleApiClient = new GoogleApiClient.Builder(mView.getActivity())
            .enableAutoManage(mView.getTarget().getActivity(), new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                    Log.d(TAG, "onConnectionFailed: google signin");
                }
            })
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();
以下是使用Android应用程序接收的访问令牌后的输出(服务器端):

[azp] => 99415613(...)s.googleusercontent.com
   [aud] => 994156(...)nss.apps.googleusercontent.com
   [sub] => 100452979093996347493
   [email] => b(...)gmail.com
   [email_verified] => 1
   [exp] => 1520621452
   [iss] => https://accounts.google.com
   [iat] => 1520617852
   [name] => M(...)e
   [picture] => https://lh3(...)hoto.jpg
   [given_name] => M(...)
   [family_name] => A(...)
   [locale] => (...)
年龄范围到底到哪里去了?

根据该页面,谷歌登录似乎没有直接在响应中返回关于对象的信息,而是
。requestProfile()
只提供了对象的附加信息

要获得所需的信息,需要使用替换了Plus API的

使用原始代码登录后,您需要创建凭据并将其与People API一起使用:

HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance(); //use some json factory of your choice

GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(context, Scopes.PROFILE);
//NOTE: You'll use the email returned by your sign in (or an Account object from the same)
credential.setSelectedAccount(new Account(email, "com.google"));

People service = new People.Builder(httpTransport, jsonFactory, credential).setApplicationName("My Application").build();
//'me' resolves of course, to the account you chose
Person person = service.people().get("people/me").execute();
person.getAgeRange();
注意:我对这一点的理解很大程度上归功于和的回答,所以你可能也想看看这些