如何在Android中使用Google People API获取Connections的个人资料ID?

时间:2017-06-24 06:45:33

标签: android google-api google-people

我正在开发应用,此应用有一项功能,需要Google帐户的个人资料ID。我可以使用代码来使用Google People API获取数据:

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

但是响应没有配置文件ID,它只有那样的联系人ID:

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

我是否遗漏了此行中包含Google个人资料ID的任何字段?setRequestMaskIncludeField("person.names,person.emailAddresses,person.phoneNumbers")

或者想从People API Google获取个人资料ID? 请帮帮我。

3 个答案:

答案 0 :(得分:0)

要获取Profile_id,您应该使用GoogleSignInOccount类,它包含在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是您的个人资料_希望这会对您有所帮助。

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

我找到了解决方案。它只是ListConnectionsResponse赋值中的“setPageSize”设置属性。完全,作业喜欢:

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