-->

How to get profile ID of Connections using Google

2019-09-15 09:03发布

问题:

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.

回答1:

For getting Profile_id you should use GoogleSignInAccount class like this it's included in 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);

    }
}

After geting result:

     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();

A person id is your profile_id hope this will help you.



回答2:

If you request "person.metadata" in your request mask. Then it will return person.metadata.sources. You can go through each source and if the source has type profile, then the source ID will be the profile ID. See documentation for more info.

Note: there may be more than one profile per contact.



回答3:

I found my solution. It 's just a "setPageSize" setting property in ListConnectionsResponse assignment. Fully, assignment likes that:

 ListConnectionsResponse response = peopleService.people().connections()
                        .list("people/me")
                        .setPageSize(1200)
                   .setRequestMaskIncludeField("person.names,person.emailAddresses,person.phoneNumbers")
                        .execute();