1

我正在实现谷歌智能锁登录以自动登录用户而无需输入,但是我遇到的问题是,即使在“成功”之后,凭证对象令牌列表中返回的令牌列表 (getIdTokens) 始终为空联系。凭证对象在什么时候实际填充了令牌列表?

我正在使用这个例子来构建代码:

https://github.com/googlesamples/android-credentials/blob/master/credentials-signin/app/src/main/java/com/google/example/credentialssignin/MainActivity.java#L101

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();
        }
}
4

2 回答 2

3

我通过添加.setIdTokenRequested(true)在 GitHub 上的 googlesamples/android-credentials 中找到了解决方案

CredentialRequest credentialRequest = new CredentialRequest.Builder()
                            .setPasswordLoginSupported(true)
                            .setAccountTypes(IdentityProviders.GOOGLE)
                            .setIdTokenRequested(true)
                            .build();
于 2017-12-26T12:13:38.533 回答
1

getIdTokens()Auth.CredentialsApi.request()当通过orAuth.CredentialsApi.getHintPickerIntent()方法检索到凭证并且凭证对应于在运行 Play Services 8+ 的设备上登录的 Google 帐户时,凭证对象上的应返回包含 OpenID Connect ID 令牌的列表

请注意,.setAccountTypes(IdentityProviders.GOOGLE)构建请求时应包括:

    CredentialRequest request = new CredentialRequest.Builder()
            .setAccountTypes(IdentityProviders.GOOGLE)
            .setSupportsPasswordLogin(true)
            .build();

或者在没有保存的凭证可用时获得提示:

    HintRequest hintRequest = new HintRequest.Builder()
            .setAccountTypes(IdentityProviders.GOOGLE)
            .setEmailAddressIdentifierSupported(true)
            .build();

Credential.Builder如果凭据是用 构造的(如问题中的部分代码所示),或者凭据与设备上 Google 帐户的电子邮件地址不匹配,则不会有 ID 令牌。

所以有些事情要检查:

  • 在运行最新版本 Play 服务 (8.4) 的设备上进行测试

  • 确保检索到的凭据与在设备上登录的 Google 帐户匹配,并且该帐户信誉良好(同步、接收电子邮件、不需要重新输入密码等)

  • 验证检索到的凭据是否先前已保存在应用程序或相关网站中(通过 Chrome 密码管理器),或者来自HintRequest内置.setAccountType(IdentityProviders.GOOGLE)

如果您仍然无法获取 ID 令牌,请在评论中留下您的环境详细信息。

于 2015-12-31T03:41:50.660 回答