簡體   English   中英

如何在Android中使用Google People API獲取Connections的配置文件ID?

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

我正在開發一個應用程序,此應用程序具有一項功能,需要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的任何想法? 請幫幫我。

為了獲取Profile_id,您應使用GoogleSignInOptions中包含的此類的GoogleSignInAccount類。

 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,希望對您有所幫助。

如果您在請求掩碼中請求"person.metadata" 然后它將返回person.metadata.sources 您可以遍歷每個源,如果源具有配置文件類型,則源ID將是配置文件ID。 請參閱文檔以獲取更多信息。

注意:每個聯系人可能有多個配置文件。

我找到了解決方案。 它只是ListConnectionsResponse分配中的“ setPageSize”設置屬性。 完全,作業像這樣:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM