我正在实现谷歌智能锁登录以自动登录用户而无需输入,但是我遇到的问题是,即使在“成功”之后,凭证对象令牌列表中返回的令牌列表 (getIdTokens) 始终为空联系。凭证对象在什么时候实际填充了令牌列表?
我正在使用这个例子来构建代码:
private void googleSilentSignIn() {
// Try silent sign-in with Google Sign In API
OptionalPendingResult<GoogleSignInResult> opr =
Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
if (opr.isDone()) {
GoogleSignInResult gsr = opr.get();
handleGoogleSignIn(gsr);
} else {
opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
@Override
public void onResult(GoogleSignInResult googleSignInResult) {
handleGoogleSignIn(googleSignInResult);
}
});
}
}
private void handleGoogleSignIn(GoogleSignInResult gsr) {
Timber.i("handleGoogleSignIn:" + (gsr == null ? "null" : gsr.getStatus()));
boolean isSignedIn = (gsr != null) && gsr.isSuccess();
if (isSignedIn) {
// Display signed-in UI
GoogleSignInAccount gsa = gsr.getSignInAccount();
String status = String.format("Signed in as %s (%s)", gsa.getDisplayName(),
gsa.getEmail());
Timber.d("handleGoogleSignIn %s", status);
// Save Google Sign In to SmartLock
Credential credential = new Credential.Builder(gsa.getEmail())
.setAccountType(IdentityProviders.GOOGLE)
.setName(gsa.getDisplayName())
.setProfilePictureUri(gsa.getPhotoUrl())
.build();
saveCredentialIfConnected(credential);
Timber.d("handleGoogleSignIn: credential tokens was %s", credential.getIdTokens().toString());
}
}
private void requestCredentials(final boolean shouldResolve, boolean onlyPasswords) {
Timber.d("requestCredentials");
CredentialRequest.Builder crBuilder = new CredentialRequest.Builder()
.setPasswordLoginSupported(true);
if (!onlyPasswords) {
crBuilder.setAccountTypes(IdentityProviders.GOOGLE);
}
Auth.CredentialsApi.request(mGoogleApiClient, crBuilder.build()).setResultCallback(
new ResultCallback<CredentialRequestResult>() {
@Override
public void onResult(CredentialRequestResult credentialRequestResult) {
Status status = credentialRequestResult.getStatus();
if (status.isSuccess()) {
// Auto sign-in success
Timber.d("requestCredentials:onsuccess with token size %d", credentialRequestResult.getCredential().getIdTokens().size() );
handleCredential(credentialRequestResult.getCredential());
} else if (status.getStatusCode() == CommonStatusCodes.RESOLUTION_REQUIRED
&& shouldResolve) {
// Getting credential needs to show some UI, start resolution
resolveResult(status, RC_CREDENTIALS_READ);
}
}
});
}
@Override
public void onStart() {
super.onStart();
if (!mIsResolving) {
requestCredentials(true /* shouldResolve */, false /* onlyPasswords */);
}
}
private void handleCredential(Credential credential) {
Timber.i("handleCredential with %s %s %s %s %s", credential.getId(), credential.getAccountType(), credential.getGeneratedPassword(), credential.getName(), credential.getPassword());
mCredential = credential;
if (IdentityProviders.GOOGLE.equals(credential.getAccountType())) {
// Google account, rebuild GoogleApiClient to set account name and then try
buildGoogleApiClient(credential.getId());
googleSilentSignIn();
}
}