我一直在关注https://developers.google.com/identity/smartlock-passwords/android/retrieve-credentials尝试自动登录用户,如果他们已将其凭据保存到 chrome 中的新 Android Smart Lock 功能。我完全按照指南进行了操作,但是我传递给 setResultCallback() 的回调没有被调用。有没有人遇到过这个问题?
没有错误消息或任何东西,它只是没有被调用。
我一直在关注https://developers.google.com/identity/smartlock-passwords/android/retrieve-credentials尝试自动登录用户,如果他们已将其凭据保存到 chrome 中的新 Android Smart Lock 功能。我完全按照指南进行了操作,但是我传递给 setResultCallback() 的回调没有被调用。有没有人遇到过这个问题?
没有错误消息或任何东西,它只是没有被调用。
问题可能是谷歌API客户端没有连接,尝试调用你activityconnect()
的onStart()
方法,或者如果你使用的是最新版本的Play Services,我们添加了API客户端的自动管理,让这更容易,真正简化了事情并避免常见问题。
只需enableAutoManage()
在构建时调用GoogleApiClient
:
// "this" is a reference to your activity
mCredentialsApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.enableAutoManage(this, this)
.addApi(Auth.CREDENTIALS_API)
.build();
然后您无需随时调用即可发出 API 请求mCredentialsApiClient.onConnect()
,Google API 客户端的生命周期将自动为您管理。例如
@Override
public void onStart() {
CredentialRequest request = new CredentialRequest.Builder()
.setSupportsPasswordLogin(true)
.build();
Auth.CredentialsApi.request(mCredentialsApiClient, request).setResultCallback(
new ResultCallback<CredentialRequestResult>() {
public void onResult(CredentialRequestResult result) {
// result.getStatus(), result.getCredential() ... sign in automatically!
...
在 Github 上查看完整的示例应用程序:https://github.com/googlesamples/android-credentials/blob/master/credentials-quickstart/app/src/main/java/com/google/example/credentialsbasic/MainActivity。爪哇
我厌倦了这里的官方演示应用程序,它起作用了。
基本上,setResultCallback()
将在 时被调用save
,request
并且delete
为了保存:
Auth.CredentialsApi.save(mCredentialsApiClient, credential).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
if (status.isSuccess()) {
Log.d(TAG, "SAVE: OK");
showToast("Credential Saved");
hideProgress();
} else {
resolveResult(status, RC_SAVE);
}
}
});
请求:
Auth.CredentialsApi.request(mCredentialsApiClient, request).setResultCallback(
new ResultCallback<CredentialRequestResult>() {
@Override
public void onResult(CredentialRequestResult credentialRequestResult) {
if (credentialRequestResult.getStatus().isSuccess()) {
// Successfully read the credential without any user interaction, this
// means there was only a single credential and the user has auto
// sign-in enabled.
processRetrievedCredential(credentialRequestResult.getCredential(), false);
hideProgress();
} else {
// Reading the credential requires a resolution, which means the user
// may be asked to pick among multiple credentials if they exist.
Status status = credentialRequestResult.getStatus();
if (status.getStatusCode() == CommonStatusCodes.SIGN_IN_REQUIRED) {
// This is a "hint" credential, which will have an ID but not
// a password. This can be used to populate the username/email
// field of a sign-up form or to initialize other services.
resolveResult(status, RC_HINT);
} else {
// This is most likely the case where the user has multiple saved
// credentials and needs to pick one
resolveResult(status, RC_READ);
}
}
}
});
对于删除:
Auth.CredentialsApi.delete(mCredentialsApiClient, mCurrentCredential).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
hideProgress();
if (status.isSuccess()) {
// Credential delete succeeded, disable the delete button because we
// cannot delete the same credential twice.
showToast("Credential Delete Success");
findViewById(R.id.button_delete_loaded_credential).setEnabled(false);
mCurrentCredential = null;
} else {
// Credential deletion either failed or was cancelled, this operation
// never gives a 'resolution' so we can display the failure message
// immediately.
Log.e(TAG, "Credential Delete: NOT OK");
showToast("Credential Delete Failed");
}
}
});
你也可以在这里克隆我的 github 中的项目,在你SHA1
的控制台中设置。
此时你应该准备好了:)