0

I'm working on an App and this app has a feature, which need profile ID of Google Account. I can get data using Google People API by using code:

 GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            // The serverClientId is an OAuth 2.0 web client ID
            .requestServerAuthCode(getString(R.string.googleWebClientId))
            .requestEmail()
            .requestScopes(new Scope(Scopes.PLUS_LOGIN),
                    new Scope(PeopleScopes.CONTACTS_READONLY),
                    new Scope(PeopleScopes.USER_EMAILS_READ),
                    new Scope(PeopleScopes.USERINFO_EMAIL),
                    new Scope(PeopleScopes.USER_PHONENUMBERS_READ))
            .build();
 // To connect with Google Play Services and Sign In
        mGoogleApiClient = new GoogleApiClient.Builder(this).
                enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
                    @Override
                    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                        Toast.makeText(AddContactUserToLeaderboardActivity.this, "Your account doesnot exists", Toast.LENGTH_LONG).show();
                    }
                }).addApi(Auth.GOOGLE_SIGN_IN_API, signInOptions).
                build();

People peopleService = setUp(AddContactUserToLeaderboardActivity.this, params[0]);
                ListConnectionsResponse response = peopleService.people().connections()
                        .list("people/me")
                        // This line's really important! Here's why:
                        // http://stackoverflow.com/questions/35604406/retrieving-information-about-a-contact-with-google-people-api-java
                        .setRequestMaskIncludeField("person.names,person.emailAddresses,person.phoneNumbers")
                        .execute();
                List<Person> connections = response.getConnections();
                Log.e(TAG, "response: " + ((GenericJson) response).toString());

But the response has no Profile ID, it just has Contact ID like that:

 {"connections":[{"etag":"%EgQBAgkL","names":[{"displayName":"A","givenName":"B","metadata":{"primary":true,"source":{"id":"5744b9050dfsfa281b","type":"CONTACT"}},...

am I missing any field to have Google Profile ID in this line?setRequestMaskIncludeField("person.names,person.emailAddresses,person.phoneNumbers")

or any idea to get Profile ID from People API Google? Help me please.

4

3 回答 3

0

要获取 Profile_id,您应该像这样使用 GoogleSignInAccount 类,它包含在 GoogleSignInOptions 中。

 public void signIn() {
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
    this.startActivityForResult(signInIntent, RC_SIGN_IN);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)  {
    //this.onActivityResult(requestCode, resultCode, data);
    super.onActivityResult(requestCode, resultCode, data);
    // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
    if (requestCode == RC_SIGN_IN) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        handleSignInResult(result);
    }
    else {
        Log.i("TAG", "request" + requestCode + " ResultCode" + resultCode + " FilterItem_Data " + data);
        mCallbackManager.onActivityResult(requestCode, resultCode, data);

    }
}

得到结果后:

     private void handleSignInResult(GoogleSignInResult result)   {
    //Log.d(TAG, "handleSignInResult:" + result.isSuccess());
    if (result.isSuccess()) {
        // Signed in successfully, show authenticated UI.
        GoogleSignInAccount acct = result.getSignInAccount();
        String personName = acct.getDisplayName();
        String personEmail = acct.getEmail();
        String personId = acct.getId();

一个人 id 是你的 profile_id 希望这会对你有所帮助。

于 2017-06-24T06:58:21.557 回答
0

如果您"person.metadata"在请求掩码中请求。然后它会返回person.metadata.sources。您可以浏览每个源,如果源具有类型配置文件,则源 ID 将是配置文件 ID。有关更多信息,请参阅文档

注意:每个联系人可能有多个个人资料。

于 2017-06-27T01:28:24.760 回答
0

我找到了我的解决方案。它只是 ListConnectionsResponse 分配中的“setPageSize”设置属性。完全,分配喜欢这样:

 ListConnectionsResponse response = peopleService.people().connections()
                        .list("people/me")
                        .setPageSize(1200)
                   .setRequestMaskIncludeField("person.names,person.emailAddresses,person.phoneNumbers")
                        .execute();
于 2017-06-29T03:37:42.393 回答