Melakukan autentikasi dengan backend menggunakan token ID
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Klien login Sekali Ketuk mengambil token ID Google saat pengguna memilih
Akun Google. Token ID adalah pernyataan identitas pengguna yang ditandatangani yang juga
berisi informasi profil dasar pengguna, mungkin termasuk alamat email
yang telah diverifikasi oleh Google.
Jika token ID tersedia, Anda dapat menggunakannya untuk mengautentikasi dengan backend aplikasi secara aman, atau untuk mendaftarkan pengguna secara otomatis ke akun baru tanpa perlu memverifikasi alamat email pengguna.
Untuk memproses login atau pendaftaran pengguna dengan token ID, kirim token ke backend
aplikasi Anda. Di backend, verifikasi token menggunakan library klien Google API atau library JWT tujuan umum. Jika pengguna belum pernah login ke aplikasi Anda dengan Akun Google ini, buat akun baru.
Jika Anda secara opsional memilih untuk menggunakan nonce untuk membantu menghindari serangan replay, gunakan
getNonce
untuk mengirimkannya bersama dengan Token ID ke server backend, dan periksa
nilai yang diharapkan. Sebaiknya Anda mempertimbangkan penggunaan nonce untuk
meningkatkan keamanan dan keselamatan pengguna.
Mendapatkan token ID dari objek kredensial
Setelah Anda mengambil kredensial pengguna, periksa apakah objek kredensial
menyertakan token ID. Jika ya, kirim ke backend Anda.
Java
publicclassYourActivityextendsAppCompatActivity{// ...privatestaticfinalintREQ_ONE_TAP=2;// Can be any integer unique to the Activity.privatebooleanshowOneTapUI=true;// ...@OverrideprotectedvoidonActivityResult(intrequestCode,intresultCode,@NullableIntentdata){super.onActivityResult(requestCode,resultCode,data);switch(requestCode){caseREQ_ONE_TAP:try{SignInCredentialcredential=oneTapClient.getSignInCredentialFromIntent(data);StringidToken=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(ApiExceptione){// ...}break;}}}
Kotlin
classYourActivity:AppCompatActivity(){// ...privatevalREQ_ONE_TAP=2// Can be any integer unique to the ActivityprivatevarshowOneTapUI=true// ...overridefunonActivityResult(requestCode:Int,resultCode:Int,data:Intent?){super.onActivityResult(requestCode,resultCode,data)when(requestCode){REQ_ONE_TAP->{try{valcredential=oneTapClient.getSignInCredentialFromIntent(data)validToken=credential.googleIdTokenwhen{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){// ...}}}// ...}
Konten dan contoh kode di halaman ini tunduk kepada lisensi yang dijelaskan dalam Lisensi Konten. Java dan OpenJDK adalah merek dagang atau merek dagang terdaftar dari Oracle dan/atau afiliasinya.
Terakhir diperbarui pada 2025-07-27 UTC.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Informasi yang saya butuhkan tidak ada","missingTheInformationINeed","thumb-down"],["Terlalu rumit/langkahnya terlalu banyak","tooComplicatedTooManySteps","thumb-down"],["Sudah usang","outOfDate","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Masalah kode / contoh","samplesCodeIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-07-27 UTC."],[],[],null,["# Authenticate with a backend using ID tokens\n\n| **Caution:** One Tap for Android is deprecated. To ensure the continued security and usability of your app, [migrate to\n| Credential Manager](/identity/sign-in/credential-manager). Credential Manager supports passkey, password, and federated identity authentication (such as Sign-in with Google), stronger security, and a more consistent user experience.\n\nThe One Tap sign-in client retrieves a Google ID token when the user selects a\nGoogle Account. An ID token is a signed assertion of a user's identity that also\ncontains a user's basic profile information, possibly including an email address\nthat has been verified by Google.\n\nWhen ID tokens are available, you can use them to securely authenticate with\nyour app's backend, or to automatically sign up the user for a new account\nwithout the need to verify the user's email address.\n\nTo sign in or sign up a user with an ID token, send the token to your app's\nbackend. On the backend, verify the token using either a Google API client\nlibrary or a general-purpose JWT library. If the user hasn't signed in to your\napp with this Google Account before, create a new account.\n\nIf you've optionally chosen to use a nonce to help avoid replay attacks, use\n[getNonce](https://developers.google.com/android/reference/com/google/android/gms/auth/api/identity/GetSignInIntentRequest#public-string-getnonce)\nto send it along with the ID Token to your backend server, and check for the\nexpected value. We recommend that you strongly consider using a nonce to\nimprove user safety and security.\n\nGet an ID token from the credentials object\n-------------------------------------------\n\nAfter you retrieve a user's credentials, check if the credentials object\nincludes an ID token. If it does, send it to your backend. \n\n### Java\n\n```java\npublic class YourActivity extends AppCompatActivity {\n\n // ...\n private static final int REQ_ONE_TAP = 2; // Can be any integer unique to the Activity.\n private boolean showOneTapUI = true;\n // ...\n\n @Override\n protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {\n super.onActivityResult(requestCode, resultCode, data);\n\n switch (requestCode) {\n case REQ_ONE_TAP:\n try {\n SignInCredential credential = oneTapClient.getSignInCredentialFromIntent(data);\n String idToken = credential.getGoogleIdToken();\n if (idToken != null) {\n // Got an ID token from Google. Use it to authenticate\n // with your backend.\n Log.d(TAG, \"Got ID token.\");\n }\n } catch (ApiException e) {\n // ...\n }\n break;\n }\n }\n}\n```\n\n### Kotlin\n\n```kotlin\nclass YourActivity : AppCompatActivity() {\n\n // ...\n private val REQ_ONE_TAP = 2 // Can be any integer unique to the Activity\n private var showOneTapUI = true\n // ...\n\n override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {\n super.onActivityResult(requestCode, resultCode, data)\n\n when (requestCode) {\n REQ_ONE_TAP -\u003e {\n try {\n val credential = oneTapClient.getSignInCredentialFromIntent(data)\n val idToken = credential.googleIdToken\n when {\n idToken != null -\u003e {\n // Got an ID token from Google. Use it to authenticate\n // with your backend.\n Log.d(TAG, \"Got ID token.\")\n }\n else -\u003e {\n // Shouldn't happen.\n Log.d(TAG, \"No ID token!\")\n }\n }\n } catch (e: ApiException) {\n // ...\n }\n }\n }\n // ...\n}\n```"]]