2017-02-23 2 views
2

Ich versuche, das Geschlecht und Geburtstag von der Google-Provider mit Firebase AuthUI zu bekommen. Hier ist mein Code.Wie erhalte ich mit FirebaseAuth Geschlecht und Geburtstag vom Google-Provider?

AuthUI.IdpConfig googleIdp = new AuthUI.IdpConfig.Builder(AuthUI.GOOGLE_PROVIDER) 
       .setPermissions(Arrays.asList(Scopes.EMAIL, Scopes.PROFILE, Scopes.PLUS_ME)) 
       .build(); 

startActivityForResult(
       AuthUI.getInstance().createSignInIntentBuilder() 
         .setLogo(R.drawable.firebase_auth_120dp) 
         .setProviders(Arrays.asList(
       new AuthUI.IdpConfig.Builder(AuthUI.EMAIL_PROVIDER).build(), 
       googleIdp)) 
         .setIsSmartLockEnabled(false) 
         .setTheme(R.style.AppTheme_Login) 
         .build(), 
       RC_SIGN_IN); 

In onActivityResult:

IdpResponse idpResponse = IdpResponse.fromResultIntent(data); 

Ich habe idpResponse, aber es enthielt nur idpSecret und idpToken. Wie kann ich auf andere angeforderte Felder für Profil wie Geschlecht und Geburtstag zugreifen? Ich kann gemeinsame Felder auf E-Mail, Name, Foto usw. mit

FirebaseAuth.getInstance().getCurrentUser(); 

Antwort

6

Firebase nicht unterstützt, dass aber Sie können das auf diese Weise tun:

Zuerst benötigten Sie client_id und client_secret.

Sie können diese beiden von Firebase Panel durch folgende Schritte erhalten:

Authentifizierung >>SIGN-IN VERFAHREN. Klicken Sie auf Google und erweitern Sie Web SDK-Konfiguration.

Gradle Abhängigkeiten:

compile 'com.google.apis:google-api-services-people:v1-rev63-1.22.0' 

Fügen Sie die folgenden Methoden in Ihrem Login-Aktivität.

private void setupGoogleAdditionalDetailsLogin() { 
       // Configure sign-in to request the user's ID, email address, and basic profile. ID and 
       // basic profile are included in DEFAULT_SIGN_IN. 
       GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
         .requestEmail() 
         .requestIdToken(GOOGLE_CLIENT_ID) 
         .requestServerAuthCode(GOOGLE_CLIENT_ID) 
         .requestScopes(new Scope("profile")) 
         .build(); 

     // Build a GoogleApiClient with access to GoogleSignIn.API and the options above. 
       mGoogleApiClient = new GoogleApiClient.Builder(this) 
         .enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() { 
          @Override 
          public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 
           Log.d(TAG, "onConnectionFailed: "); 
          } 
         }) 
         .addApi(Auth.GOOGLE_SIGN_IN_API, gso) 
         .build(); 
      } 

    public void googleAdditionalDetailsResult(Intent data) { 
      Log.d(TAG, "googleAdditionalDetailsResult: "); 
      GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); 
      if (result.isSuccess()) { 
       // Signed in successfully 
       GoogleSignInAccount acct = result.getSignInAccount(); 
       // execute AsyncTask to get data from Google People API 
       new GoogleAdditionalDetailsTask().execute(acct); 
      } else { 
       Log.d(TAG, "googleAdditionalDetailsResult: fail"); 
       startHomeActivity(); 
      } 
     } 

    private void startGoogleAdditionalRequest() { 
      Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); 
      startActivityForResult(signInIntent, RC_SIGN_GOOGLE); 
     } 

Async Aufgabe zusätzliche Details

public class GoogleAdditionalDetailsTask extends AsyncTask<GoogleSignInAccount, Void, Person> { 
     @Override 
     protected Person doInBackground(GoogleSignInAccount... googleSignInAccounts) { 
      Person profile = null; 
      try { 
       HttpTransport httpTransport = new NetHttpTransport(); 
       JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance(); 

       //Redirect URL for web based applications. 
       // Can be empty too. 
       String redirectUrl = "urn:ietf:wg:oauth:2.0:oob"; 

       // Exchange auth code for access token 
       GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(
         httpTransport, 
         jsonFactory, 
         GOOGLE_CLIENT_ID, 
         GOOGLE_CLIENT_SECRET, 
         googleSignInAccounts[0].getServerAuthCode(), 
         redirectUrl 
       ).execute(); 

       GoogleCredential credential = new GoogleCredential.Builder() 
         .setClientSecrets(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET) 
         .setTransport(httpTransport) 
         .setJsonFactory(jsonFactory) 
         .build(); 

       credential.setFromTokenResponse(tokenResponse); 

       People peopleService = new People.Builder(httpTransport, jsonFactory, credential) 
         .setApplicationName(App.getInstance().getString(R.string.app_name)) 
         .build(); 

       // Get the user's profile 
       profile = peopleService.people().get("people/me").execute(); 
      } catch (IOException e) { 
       Log.d(TAG, "doInBackground: " + e.getMessage()); 
       e.printStackTrace(); 
      } 
      return profile; 
     } 

     @Override 
     protected void onPostExecute(Person person) { 
      if (person != null) { 
       if (person.getGenders() != null && person.getGenders().size() > 0) { 
        profileGender = person.getGenders().get(0).getValue(); 
       } 
       if (person.getBirthdays() != null && person.getBirthdays().get(0).size() > 0) { 
//     yyyy-MM-dd 
        Date dobDate = person.getBirthdays().get(0).getDate(); 
        if (dobDate.getYear() != null) { 
         profileBirthday = dobDate.getYear() + "-" + dobDate.getMonth() + "-" + dobDate.getDay(); 
         profileYearOfBirth = DateHelper.getYearFromGoogleDate(profileBirthday); 
        } 
       } 
       if (person.getBiographies() != null && person.getBiographies().size() > 0) { 
        profileAbout = person.getBiographies().get(0).getValue(); 
       } 
       if (person.getCoverPhotos() != null && person.getCoverPhotos().size() > 0) { 
        profileCover = person.getCoverPhotos().get(0).getUrl(); 
       } 
       Log.d(TAG, String.format("googleOnComplete: gender: %s, birthday: %s, about: %s, cover: %s", profileGender, profileBirthday, profileAbout, profileCover)); 
      } 
      startHomeActivity(); 
     } 
    } 

ändern erhalten Sie dieses mögen onActivityResult:

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (requestCode == RC_SIGN_GOOGLE) { // result for addition details request 
      googleAdditionalDetailsResult(data); 
      return; 
     } else if (requestCode == RC_SIGN_IN && resultCode == RESULT_OK) { //logged in with firebase 
      if (FirebaseAuth.getInstance().getCurrentUser().getProviders().get(0).equals("google.com")) { 
      // user logged in with google account using firebase ui 
       startGoogleAdditionalRequest(); 
      } else { 
      // user logged in with google 
       startHomeActivity(); 
      } 
     } else { 
      // handle error 
     } 
    } 

Update: wenn gibt Codefehler

p ersonFields Maske erforderlich

dann folgenden Code verwenden:

profile = peopleService.people().get("people/me"). setRequestMaskIncludeField("person.names,person.emailAddress‌​es,person.genders,pe‌​rson.birthdays").exe‌​cute(); 

Dank @AbrahamGharyali.

+0

Danke. Gut erklärt und es funktioniert perfekt. – William

+0

Dieser Code gibt den Fehler personFields Maske erforderlich ist. Ich habe es so gemacht. ich hoffe es hilft. '// Erhalte das Profil des Benutzers profile = peopleService.people(). Get (" people/me "). setRequestMaskIncludeField ("person.names, person.emailAddresses, person.guters, person.geburtstags"). Execute(); ' –

+0

@AbrahamGharyali danke für den Vorschlag, dass ich es aktualisieren werde. – Sabeeh

1

Leider Firebase hat keine integrierte Funktionalität des Benutzers Geschlecht/Geburtsdatum nach erfolgreicher Anmeldung zu erhalten. Sie müssten diese Daten von jedem der Provider selbst abrufen.

Hier ist, wie Sie vielleicht den Benutzer Geschlecht von Google mit Google Menschen API

public class SignInActivity extends AppCompatActivity implements 
    GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener, 
    View.OnClickListener { 
private static final int RC_SIGN_IN = 9001; 

private GoogleApiClient mGoogleApiClient; 

private FirebaseAuth mAuth; 
private FirebaseAuth.AuthStateListener mAuthListener; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_google_sign_in); 

    // We can only get basic information using FirebaseAuth 
    mAuth = FirebaseAuth.getInstance(); 
    mAuthListener = new FirebaseAuth.AuthStateListener() { 
     @Override 
     public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { 
      FirebaseUser user = firebaseAuth.getCurrentUser(); 
      if (user != null) { 
       // User is signed in to Firebase, but we can only get 
       // basic info like name, email, and profile photo url 
       String name = user.getDisplayName(); 
       String email = user.getEmail(); 
       Uri photoUrl = user.getPhotoUrl(); 

       // Even a user's provider-specific profile information 
       // only reveals basic information 
       for (UserInfo profile : user.getProviderData()) { 
        // Id of the provider (ex: google.com) 
        String providerId = profile.getProviderId(); 
        // UID specific to the provider 
        String profileUid = profile.getUid(); 
        // Name, email address, and profile photo Url 
        String profileDisplayName = profile.getDisplayName(); 
        String profileEmail = profile.getEmail(); 
        Uri profilePhotoUrl = profile.getPhotoUrl(); 
       } 
      } else { 
       // User is signed out of Firebase 
      } 
     } 
    }; 

    // Google sign-in button listener 
    findViewById(R.id.google_sign_in_button).setOnClickListener(this); 

    // Configure GoogleSignInOptions 
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
      .requestIdToken(getString(R.string.server_client_id)) 
      .requestServerAuthCode(getString(R.string.server_client_id)) 
      .requestEmail() 
      .requestScopes(new Scope(PeopleScopes.USERINFO_PROFILE)) 
      .build(); 

    // Build a GoogleApiClient with access to the Google Sign-In API and the 
    // options specified by gso. 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .enableAutoManage(this, this) 
      .addOnConnectionFailedListener(this) 
      .addConnectionCallbacks(this) 
      .addApi(Auth.GOOGLE_SIGN_IN_API, gso) 
      .build(); 
} 

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
     case R.id.google_sign_in_button: 
      signIn(); 
      break; 
    } 
} 

private void signIn() { 
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); 
    startActivityForResult(signInIntent, RC_SIGN_IN); 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent 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); 
     if (result.isSuccess()) { 
      // Signed in successfully 
      GoogleSignInAccount acct = result.getSignInAccount(); 

      // execute AsyncTask to get gender from Google People API 
      new GetGendersTask().execute(acct); 

      // Google Sign In was successful, authenticate with Firebase 
      firebaseAuthWithGoogle(acct); 
     } 
    } 
} 

class GetGendersTask extends AsyncTask<GoogleSignInAccount, Void, List<Gender>> { 
    @Override 
    protected List<Gender> doInBackground(GoogleSignInAccount... googleSignInAccounts) { 
     List<Gender> genderList = new ArrayList<>(); 
     try { 
      HttpTransport httpTransport = new NetHttpTransport(); 
      JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance(); 

      //Redirect URL for web based applications. 
      // Can be empty too. 
      String redirectUrl = "urn:ietf:wg:oauth:2.0:oob"; 

      // Exchange auth code for access token 
      GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(
        httpTransport, 
        jsonFactory, 
        getApplicationContext().getString(R.string.server_client_id), 
        getApplicationContext().getString(R.string.server_client_secret), 
        googleSignInAccounts[0].getServerAuthCode(), 
        redirectUrl 
      ).execute(); 

      GoogleCredential credential = new GoogleCredential.Builder() 
        .setClientSecrets(
         getApplicationContext().getString(R.string.server_client_id), 
         getApplicationContext().getString(R.string.server_client_secret) 
        ) 
        .setTransport(httpTransport) 
        .setJsonFactory(jsonFactory) 
        .build(); 

      credential.setFromTokenResponse(tokenResponse); 

      People peopleService = new People.Builder(httpTransport, jsonFactory, credential) 
        .setApplicationName("My Application Name") 
        .build(); 

      // Get the user's profile 
      Person profile = peopleService.people().get("people/me").execute(); 
      genderList.addAll(profile.getGenders()); 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return genderList; 
    } 

    @Override 
    protected void onPostExecute(List<Gender> genders) { 
     super.onPostExecute(genders); 
     // iterate through the list of Genders to 
     // get the gender value (male, female, other) 
     for (Gender gender : genders) { 
      String genderValue = gender.getValue(); 
     } 
    } 
} 

}

bekommen Sie mehr Informationen über Zugriff auf Google APIs finden

+0

Hallo vishnu danke für die Antwort. Kannst du mir bitte Import für HttpTransport, JacksonFactory und alle anderen, die hier verwendet werden. – William

+0

Hallo William fügen Sie die folgenden Abhängigkeiten hinzu, kompilieren Sie 'com.google.api-client: google-api-client: 1.20.0' kompilieren Sie 'com.google.firebase: firebase-auth: 10.0.1' kompilieren 'com. google.android.gms: play-services-auth: 10.0.1 ', füge den Klassenpfad-Klassenpfad' com.google.gms: google-services: 3.0.0 'hinzu und füge das Apply-Plugin hinzu:' com.google.gms.google- Dienste 'unterhalb der Abhängigkeit –

Verwandte Themen