使用者選取 Google 帳戶時,One Tap 登入用戶端會擷取 Google ID 權杖。ID 權杖是使用者身分的簽署斷言,其中還包含使用者的個人資料基本資訊,可能包括經過 Google 驗證的電子郵件地址。
取得 ID 權杖後,您可以使用這些權杖安全地驗證應用程式的後端,或是自動為使用者申請新帳戶,而無需驗證使用者的電子郵件地址。
如要使用 ID 權杖登入或註冊使用者,請將權杖傳送至應用程式的後端。在後端,請使用 Google API 用戶端程式庫或通用 JWT 程式庫驗證權杖。如果使用者未曾使用這個 Google 帳戶登入應用程式,請建立新帳戶。
如果您選擇使用 Nonce 來避免重播攻擊,請使用 getNonce 將 Nonce 和 ID 權杖一併傳送至後端伺服器,並檢查預期值。強烈建議您考慮使用 nonce,以提升使用者安全性。
從憑證物件取得 ID 權杖
擷取使用者的憑證後,請檢查憑證物件是否包含 ID 權杖。如果是,請將其傳送至後端。
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){// ...}}}// ...}
[[["容易理解","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 (世界標準時間)。"],[],[],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```"]]