Esegui l'autenticazione con un backend usando i token ID

Il client di accesso One Tap recupera un token ID Google quando l'utente seleziona un Account Google. Un token ID è un'asserzione firmata dell'identità di un utente che contiene anche le informazioni di base del profilo dell'utente, possibilmente incluso un indirizzo email verificato da Google.

Quando i token ID sono disponibili, puoi utilizzarli per eseguire l'autenticazione in modo sicuro con il backend della tua app o per registrare automaticamente l'utente per un nuovo account senza la necessità di verificare il suo indirizzo email.

Per accedere o registrare un utente con un token ID, invia il token al backend della tua app. Nel backend, verifica il token utilizzando una libreria client delle API di Google o una libreria JWT per uso generico. Se l'utente non ha mai eseguito l'accesso alla tua app con questo Account Google, crea un nuovo account.

Se hai scelto facoltativamente di utilizzare un nonce per evitare attacchi di replay, utilizza getNonce per inviarlo insieme al token ID al server di backend e verifica il valore previsto. Ti consigliamo vivamente di prendere in considerazione l'utilizzo di un nonce per migliorare la sicurezza degli utenti.

Recuperare un token ID dall'oggetto delle credenziali

Dopo aver recuperato le credenziali di un utente, controlla se l'oggetto delle credenziali include un token ID. In tal caso, invialo al backend.

Java

public class YourActivity extends AppCompatActivity {

  // ...
  private static final int REQ_ONE_TAP = 2;  // Can be any integer unique to the Activity.
  private boolean showOneTapUI = true;
  // ...

  @Override
  protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
      super.onActivityResult(requestCode, resultCode, data);

      switch (requestCode) {
          case REQ_ONE_TAP:
              try {
                  SignInCredential credential = oneTapClient.getSignInCredentialFromIntent(data);
                  String idToken = credential.getGoogleIdToken();
                  if (idToken !=  null) {
                      // Got an ID token from Google. Use it to authenticate
                      // with your backend.
                      Log.d(TAG, "Got ID token.");
                  }
              } catch (ApiException e) {
                  // ...
              }
              break;
      }
  }
}

Kotlin

class YourActivity : AppCompatActivity() {

    // ...
    private val REQ_ONE_TAP = 2  // Can be any integer unique to the Activity
    private var showOneTapUI = true
    // ...

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        when (requestCode) {
             REQ_ONE_TAP -> {
                try {
                    val credential = oneTapClient.getSignInCredentialFromIntent(data)
                    val idToken = credential.googleIdToken
                    when {
                        idToken != null -> {
                            // Got an ID token from Google. Use it to authenticate
                            // with your backend.
                            Log.d(TAG, "Got ID token.")
                        }
                        else -> {
                            // Shouldn't happen.
                            Log.d(TAG, "No ID token!")
                        }
                    }
                } catch (e: ApiException) {
                    // ...
            }
        }
    }
    // ...
}