ไคลเอ็นต์การลงชื่อเข้าใช้ด้วย One Tap จะดึงข้อมูลโทเค็นรหัส Google เมื่อผู้ใช้เลือกบัญชี Google โทเค็นระบุตัวตนคือการยืนยันที่ลงนามของข้อมูลระบุตัวตนของผู้ใช้ซึ่งมีข้อมูลโปรไฟล์พื้นฐานของผู้ใช้ด้วย ซึ่งอาจรวมถึงอีเมลที่ Google ยืนยันแล้ว
หากต้องการลงชื่อเข้าใช้หรือลงชื่อสมัครใช้ผู้ใช้ด้วยโทเค็นระบุตัวตน ให้ส่งโทเค็นไปยังแบ็กเอนด์ของแอป ในแบ็กเอนด์ ให้ยืนยันโทเค็นโดยใช้ไลบรารีไคลเอ็นต์ Google API หรือไลบรารี JWT อเนกประสงค์ หากผู้ใช้ไม่เคยลงชื่อเข้าใช้แอปด้วยบัญชี Google นี้มาก่อน ให้สร้างบัญชีใหม่
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){// ...}}}// ...}
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 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```"]]