虽然明确的请求,但无法从Google Plus帐户获得私人生日 [英] Cannot get private birthday from Google Plus account although explicit request

查看:543
本文介绍了虽然明确的请求,但无法从Google Plus帐户获得私人生日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从Google Plus帐户中获取用户的信息,包括性别和年龄。由于这些领域可能是私人的,我认为明确要求他们可以解决问题。但是,尽管登录对话框明确指出应用程序请求查看完整的出生日期,但我未能获得生日。



这些是我的范围(尝试过很多变体):

  GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions。 DEFAULT_SIGN_IN)
//.requestScopes(new Scope(Scopes.PROFILE))
//.requestScopes(new Scope(Scopes.PLUS_LOGIN))
.requestScopes(new Scope(https:/ /www.googleapis.com/auth/user.birthday.read),
新范围(https://www.googleapis.com/auth/userinfo.profile))
//。 requestProfile()
.requestEmail()
.build();

mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this,this)
.addApi(Auth.GOOGLE_SIGN_IN_API,gso)
.addApi(Plus。 API)
.addScope(new Scope(Scopes.PROFILE))
.build();

在<$中使用弃用的 getCurrentPerson c $ c> onActivityResult ,我得到空值:

  if(requestCode == RC_SIGN_IN){ 
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);

if(mGoogleApiClient.hasConnectedApi(Plus.API)){
Person person = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
if(person!= null){
Log.i(TAG,person.getDisplayName()); //成功返回全名
Log.i(TAG,person.getGender()); // 0
Log.i(TAG,person.getBirthday()); // null
}
}
}

我也试过( GoogleSignInAccount account = result.getSignInAccount()),但据我搜索,这个变量根本不拥有请求的数据。



我错过了什么吗?或者,也许这是不可能获得私人数据,虽然显式请求?



谢谢。



顺便说一下,我没有尝试过一个私人性别的场景。

/ people / v1 / how-tos / authorizingrel =nofollow noreferrer>此Google People API文档将有助于解决您的问题。

请请注意:


如果请求需要授权(例如申请
个人的私人 data),那么应用程序必须为请求提供 OAuth
2.0标记。应用程序也可能提供API密钥,但它不必。



非公开用户数据的People API请求必须由经过身份验证的用户授权。



...


  1. 如果用户批准,Google会为您的应用程序提供短暂的访问令牌

  2. 您的应用程序请求用户数据,并将访问令牌附加到请求中。

  3. 如果Google确定
    您的请求和令牌有效,它会返回请求的数据。


如果您的应用没有访问令牌,则可以阅读



其他有用的链接:

Google Developers Blog - 宣布People API



Google People API相关的Google API



向Google授权REST API(另一种获取访问令牌的方式)


I am trying to get the user's information, including gender and age, from its Google Plus account. Since these fields may be private, I thought that requesting them explicitly would solve the problem. However, although the sign in dialog states explicitly that the app requests to View your complete date of birth, I fail to get the birthday.

These are my scopes (tried many variations):

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        //.requestScopes(new Scope(Scopes.PROFILE))
        //.requestScopes(new Scope(Scopes.PLUS_LOGIN))
        .requestScopes(new Scope("https://www.googleapis.com/auth/user.birthday.read"),
                new Scope("https://www.googleapis.com/auth/userinfo.profile"))
        //.requestProfile()
        .requestEmail()
        .build();

mGoogleApiClient = new GoogleApiClient.Builder(this)
        .enableAutoManage(this, this)
        .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
        .addApi(Plus.API)
        .addScope(new Scope(Scopes.PROFILE))
        .build();

When using the deprecated getCurrentPerson in onActivityResult, I get null value:

if (requestCode == RC_SIGN_IN) {
    GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);

    if (mGoogleApiClient.hasConnectedApi(Plus.API)) {
        Person person  = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
        if (person != null) {
            Log.i(TAG, person.getDisplayName());    //returns full name successfully
            Log.i(TAG, person.getGender());         //0
            Log.i(TAG, person.getBirthday());       //null
        }
    }
}

I also tried to get it from the account (GoogleSignInAccount account = result.getSignInAccount()) but as far as I've searched, this variable doesn't own the requested data at all.

Am I missing something? Or maybe it's impossible to get private data although explicit request?

Thanks.

BTW, I haven't tried yet a scenario with a private gender.

解决方案

I think this Google People API documentation will be helpful for your issue.

Please pay attention to:

If the request requires authorization (such as a request for an individual's private data), then the application must provide an OAuth 2.0 token with the request. The application may also provide the API key, but it doesn't have to.

and

Requests to the People API for non-public user data must be authorized by an authenticated user.

...

  1. If the user approves, then Google gives your application a short-lived access token.
  2. Your application requests user data, attaching the access token to the request.
  3. If Google determines that your request and the token are valid, it returns the requested data.

If your app has not got an access token, you can read Using OAuth 2.0 to Access Google APIs or try my answer at the following question:

How to get access token after user is signed in from Gmail in Android?


UPDATE: Supposing that your app can get the access token by using the sample code in my answer above, then inside onResponse, add more snippets as below:

...
@Override
public void onResponse(Response response) throws IOException {
    try {
        JSONObject jsonObject = new JSONObject(response.body().string());
        final String message = jsonObject.toString(5);
        Log.i("onResponse", message);

        // FROM HERE...
        String accessToken = jsonObject.optString("access_token");

        OkHttpClient client2 = new OkHttpClient();
        final Request request2 = new Request.Builder()
                .url("https://people.googleapis.com/v1/people/me")
                .addHeader("Authorization", "Bearer " + accessToken)
                .build();

        client2.newCall(request2).enqueue(new Callback() {
            @Override
            public void onFailure(final Request request, final IOException e) {
                Log.e("onFailure", e.toString());
            }

            @Override
            public void onResponse(Response response) throws IOException {
                try {
                    JSONObject jsonObject = new JSONObject(response.body().string());
                    final String message = jsonObject.toString(5);
                    Log.i("onResponse", message);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
...

Logcat info (I truncated since it's long)

I/onResponse: {
                   "photos": [
                        {
                             "url": "https:...photo.jpg",
                             "metadata": {
                                  "source": {
                                       "id": "1....774",
                                       "type": "PROFILE"
                                  },
                                  "primary": true
                             }
                        }
                   ],
                   ...                                                                               
                   "birthdays": [
                        {
                             "date": {
                                  "month": 2,
                                  "year": 1980,
                                  "day": 2
                             },
                             "metadata": {
                                  "source": {
                                       "id": "1....774",
                                       "type": "PROFILE"
                                  },
                                  "primary": true
                             }
                        }
                   ],
                   ....
              }

GoogleSignInOptions and GoogleApiClient:

String serverClientId = getString(R.string.server_client_id);
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestScopes(new Scope("https://www.googleapis.com/auth/user.birthday.read"))
        .requestServerAuthCode(serverClientId)
        .build();

GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
        .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
        .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
        .build();

Please note that the OkHttp version I use here is v2.6.0, if your app uses the newest (3.3.1), then the syntax/classes will be different. Of course, you can also use the others such as Volley, Retrofit...


Moreover, this Google's People API - Method people.get provides Try It as screenshot below

Other useful links:

Google Developers Blog - Announcing the People API

Google APIs related to Google People API

Authorizing with Google for REST APIs (another way to get access token)

这篇关于虽然明确的请求,但无法从Google Plus帐户获得私人生日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆