简体   繁体   中英

How to get profile ID of Connections using Google People API in Android?

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.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM