2017-06-24 3 views
0

Ich arbeite an einer App und diese App hat eine Funktion, die Profil ID des Google-Kontos benötigt. Ich kann Daten mit Google Menschen API erhalten, indem Code verwendet:Wie erhält man die Profil-ID von Connections mit Google People API in Android?

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

Aber die Antwort hat keine Profil-ID, es hat nur Kontakt ID wie folgt aus:

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

bin ich jedes Feld fehlt Google haben Profil-ID in dieser Zeile? setRequestMaskIncludeField("person.names,person.emailAddresses,person.phoneNumbers")

oder eine Idee, Profil-ID von People API Google zu bekommen? Hilf mir bitte.

Antwort

0

Um Profile_id zu erhalten, sollten Sie die GoogleSignInAccount-Klasse verwenden, so wie sie in GoogleSignInOptions enthalten ist.

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

    } 
} 

Nach Geting Ergebnis:

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

Eine Person id ist Ihr profile_id diese Hoffnung wird Ihnen helfen.

+0

ich meine Profil ID des zugehörigen Kontakts mit meinem Konto. –

0

Wenn Sie in Ihrer Anfrage-Maske "person.metadata" anfordern. Dann wird es person.metadata.sources zurückgeben. Sie können jede Quelle durchsuchen und wenn die Quelle ein Typprofil hat, ist die Quell-ID die Profil-ID. Weitere Informationen finden Sie unter documentation.

Hinweis: Pro Kontakt kann mehr als ein Profil vorhanden sein.

+0

Ich habe versucht, Ihre Lösung, aber es gibt keine Änderung im Ergebnis von google people :( –

+0

Sind Sie sicher, dass der Kontakt mit einem Profil verknüpft ist? Der Kontakt muss eine E-Mail, Telefonnummer oder Profil-URL, die durch das Profil bestätigt Sie können überprüfen, indem Sie auf https://contacts.google.com/ gehen und sehen, ob es ein Profil für diesen Kontakt zeigt –

0

Ich fand meine Lösung. Es ist nur eine Einstellungseigenschaft "setPageSize" in der ListConnectionsResponse-Zuweisung. Vollständig, Zuordnung mag, dass:

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

Wenn Sie alle Kontakte möchten, sollten Sie weitere Aufrufe mit dem zurückgegebenen pageToken in der Antwort zu erhalten alle Seiten von Kontakten. –

Verwandte Themen