[[["容易理解","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-08-31 (世界標準時間)。"],[[["\u003cp\u003eSmart Lock for Passwords is deprecated; migrate to Credential Manager for enhanced security and user experience with passkey, password, and federated identity support.\u003c/p\u003e\n"],["\u003cp\u003eUse the Credentials API to automatically sign in users by requesting and retrieving their stored credentials.\u003c/p\u003e\n"],["\u003cp\u003eHandle successful credential requests by identifying the credential type and completing the appropriate sign-in process, like Google Sign-In or password-based sign-in.\u003c/p\u003e\n"],["\u003cp\u003eWhen multiple saved credentials exist, handle the \u003ccode\u003eResolvableApiException\u003c/code\u003e to prompt the user to choose an account and retrieve the selected credentials in \u003ccode\u003eonActivityResult()\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eIf stored credentials are not found, guide users through account creation or manual sign-in, potentially using sign-in hints to expedite the process.\u003c/p\u003e\n"]]],[],null,["# Retrieve a user's stored credentials\n\n| **Deprecated:** Smart Lock for Passwords is deprecated. To ensure the continued security and usability of your app, [migrate to\n| Credential Manager](https://developer.android.com/training/sign-in/passkeys/) today. Credential Manager supports passkey, password, and federated identity authentication (such as Sign-in with Google), stronger security, and a more consistent user experience.\n\nAutomatically sign users into your app by using the Credentials API to request\nand retrieve stored credentials for your users.\n\nBefore you begin\n----------------\n\n[Configure an Android Studio project](/identity/smartlock-passwords/android/get-started).\n\nCreate a CredentialsClient object\n---------------------------------\n\nTo request stored credentials, you must create an instance of\n[`CredentialsClient`](/android/reference/com/google/android/gms/auth/api/credentials/CredentialsClient) to access the Credentials API: \n\n CredentialsClient mCredentialsClient;\n\n // ...\n\n mCredentialsApiClient = Credentials.getClient(this);\n\nCreate a CredentialRequest object\n---------------------------------\n\nA [`CredentialRequest`](/android/reference/com/google/android/gms/auth/api/credentials/CredentialRequest) object specifies the\nsign-in systems from which you want to request credentials. Build a\n`CredentialRequest` using the `setPasswordLoginSupported` method for\npassword-based sign-in, and the `setAccountTypes()` method for federated\nsign-in services such as Google Sign-In. \n\n mCredentialRequest = new CredentialRequest.Builder()\n .setPasswordLoginSupported(true)\n .setAccountTypes(IdentityProviders.GOOGLE, IdentityProviders.TWITTER)\n .build();\n\nUse the constants defined in [`IdentityProviders`](/android/reference/com/google/android/gms/auth/api/credentials/IdentityProviders)\nto specify commonly-used sign-in providers. For other sign-in providers, use any\nstring that uniquely identifies the provider. You must use the same provider identifier\nto store credentials as you use to retrieve the credentials.\n\nRequest stored credentials\n--------------------------\n\nAfter you have created `CredentialsClient` and `CredentialRequest` objects, pass the request object\nto the [`CredentialsClient.request()`](/android/reference/com/google/android/gms/auth/api/credentials/CredentialsClient#request(com.google.android.gms.auth.api.credentials.CredentialRequest))\nmethod to request credentials stored for your app. \n\n mCredentialsClient.request(mCredentialRequest).addOnCompleteListener(\n new OnCompleteListener\u003cCredentialRequestResponse\u003e() {\n @Override\n public void onComplete(@NonNull Task\u003cCredentialRequestResponse\u003e task) {\n\n if (task.isSuccessful()) {\n // See \"Handle successful credential requests\"\n onCredentialRetrieved(task.getResult().getCredential());\n return;\n }\n\n // See \"Handle unsuccessful and incomplete credential requests\"\n // ...\n }\n });\n\nDefine a callback to handle successful and failed requests using the\n`addOnCompleteListener()` method.\n\nHandle successful credential requests\n-------------------------------------\n\n\nOn a successful credential request, use the resulting\n[`Credential`](/android/reference/com/google/android/gms/auth/api/credentials/Credential)\nobject to complete the user's sign-in to your app. Use the `getAccountType()` method\nto determine the type of retrieved credentials, then complete the appropriate sign-in\nprocess. For example, for Google Sign-In, create a `GoogleSignInClient` object that\nincludes the user's ID, then use the object to start the sign-in flow. For password-based\nsign-in, use the user's ID and password from the Credential object to complete your app's\nsign-in process. \n\n private void onCredentialRetrieved(Credential credential) {\n String accountType = credential.getAccountType();\n if (accountType == null) {\n // Sign the user in with information from the Credential.\n signInWithPassword(credential.getId(), credential.getPassword());\n } else if (accountType.equals(IdentityProviders.GOOGLE)) {\n // The user has previously signed in with Google Sign-In. Silently\n // sign in the user with the same ID.\n // See https://developers.google.com/identity/sign-in/android/\n GoogleSignInOptions gso =\n new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)\n .requestEmail()\n .build();\n\n GoogleSignInClient signInClient = GoogleSignIn.getClient(this, gso);\n Task\u003cGoogleSignInAccount\u003e task = signInClient.silentSignIn();\n // ...\n }\n }\n\n| If the ID of a retrieved credential matches the ID of an account signed-in of the device, the credential will contain an ID token that you can use to authenticate without a password. See [Sign In Using ID Tokens](/identity/smartlock-passwords/android/idtoken-auth).\n\nHandle multiple saved credentials\n---------------------------------\n\nWhen user input is required to select a credential, the `request()` task will\nfail with a [`ResolvableApiException`](/android/reference/com/google/android/gms/common/api/ResolvableApiException). Check that\n`getStatusCode()` returns `RESOLUTION_REQUIRED` and\ncall the exception's `startResolutionForResult()` method to prompt the user\nto choose an account. Then, retrieve the user's chosen credentials from the\nactivity's `onActivityResult()` method by passing `Credential.EXTRA_KEY` to the\n[`getParcelableExtra()`](https://developer.android.com/reference/android/content/Intent.html#getParcelableExtra(java.lang.String))\nmethod. \n\n```transact-sql\nmCredentialsClient.request(request).addOnCompleteListener(\n new OnCompleteListener() {\n @Override\n public void onComplete(@NonNull Task task) {\n if (task.isSuccessful()) {\n // ...\n return;\n }\n\n Exception e = task.getException();\n if (e instanceof ResolvableApiException) {\n // This is most likely the case where the user has multiple saved\n // credentials and needs to pick one. This requires showing UI to\n // resolve the read request.\n ResolvableApiException rae = (ResolvableApiException) e;\n resolveResult(rae, RC_READ);\n } else if (e instanceof ApiException) {\n // The user must create an account or sign in manually.\n Log.e(TAG, \"Unsuccessful credential request.\", e);\n\n ApiException ae = (ApiException) e;\n int code = ae.getStatusCode();\n // ...\n }\n }\n });\n``` \n\n private void resolveResult(ResolvableApiException rae, int requestCode) {\n try {\n rae.startResolutionForResult(MainActivity.this, requestCode);\n mIsResolving = true;\n } catch (IntentSender.SendIntentException e) {\n Log.e(TAG, \"Failed to send resolution.\", e);\n hideProgress();\n }\n }\n\n @Override\n public void onActivityResult(int requestCode, int resultCode, Intent data) {\n super.onActivityResult(requestCode, resultCode, data);\n\n // ...\n\n if (requestCode == RC_READ) {\n if (resultCode == RESULT_OK) {\n Credential credential = data.getParcelableExtra(Credential.EXTRA_KEY);\n onCredentialRetrieved(credential);\n } else {\n Log.e(TAG, \"Credential Read: NOT OK\");\n Toast.makeText(this, \"Credential Read Failed\", Toast.LENGTH_SHORT).show();\n }\n }\n\n // ...\n\n }\n\nWhen stored credentials are not found, users must create an account or manually\nsign in. If [`getStatusCode()`](/android/reference/com/google/android/gms/common/api/ApiException#getStatusCode())\nreturns [`SIGN_IN_REQUIRED`](/android/reference/com/google/android/gms/common/api/CommonStatusCodes#SIGN_IN_REQUIRED),\nyou can optionally expedite the sign-up and sign-in processes by prompting the\nuser to choose recently used sign-in information, such as email address and\nname, and automatically filling some fields of the forms with that information.\nSee [Provide sign-in hints to a user](/identity/smartlock-passwords/android/retrieve-hints)\nfor details.\n\nOn successful sign in, allow users to [save their credentials](/identity/smartlock-passwords/android/store-credentials)\nto automate future authentication on all their devices."]]