CredentialRequest request = new CredentialRequest.Builder() .setSupportsPasswordLogin(true) .setAccountTypes(IdentityProviders.GOOGLE) // you can add other identity providers, too .build(); Auth.CredentialsApi.request(mCredentialsApiClient, request).setResultCallback( new ResultCallback<CredentialRequestResult>() { public void onResult(CredentialRequestResult result) { if (result.getStatus().isSuccess()) { handleCredential(result.getCredential()) // sign in automatically!
HintRequest hintRequest = new HintRequest.Builder() .setEmailAddressIdentifierSupported(true) .setAccountTypes(IdentityProviders.GOOGLE) .build(); PendingIntent intent = Auth.CredentialsApi.getHintPickerIntent(mCredentialsApiClient, hintRequest); startIntentSenderForResult(intent.getIntentSender(), RC_HINT, null, 0, 0, 0); ... onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case RC_HINT: if (resultCode == RESULT_OK) { Credential hint = data.getParcelableExtra(Credential.EXTRA_KEY); handleCredential(hint);
handleCredential(Credential credential) { if (!credential.getIdTokens().isEmpty()) { credential.getIdTokens().get(0).getIdToken(); // send the ID token string to your server } else { // otherwise, try fill the sign-in form fields and submit if password is available mEmailField.setText(credential.getId()); mPasswordField.setText(credential.getPassword());
GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(transport, jsonFactory) .setIssuer("https://accounts.google.com") .setAudience(Arrays.asList(String.format("android://%s@%s", SHA512_HASH, PACKAGE_NAME))) .build(); ... GoogleIdToken idToken = verifier.verify(idTokenString); if (idToken == null) { Log.w(TAG, "ID Token Verification Failed, check the README for instructions."); return; } GoogleIdToken.Payload payload = idToken.getPayload(); Log.d(TAG, "IdToken:Email:" + payload.getEmail()); Log.d(TAG, "IdToken:EmailVerified:" + payload.getEmailVerified()); // based on the email address, determine whether you need to create account // or just sign user in
Credential credential = new Credential.Builder(hint) // if you signed in with ID token, // set account type to the URL for your app (instead of a password field) //.setAccountType("https://yourdomain.com") .setPassword(password) .build(); Auth.CredentialsApi.save(mCredentialsApiClient, credential).setResultCallback( new ResolvingResultCallbacks<Status>(this, RC_SAVE) {