Android:Google 登录 - 凭据 token 列表始终为空

标签 android google-signin android-googleapiclient google-smartlockpasswords googlesigninaccount

我正在实现谷歌智能锁登录以自动登录用户而无需输入,但是我遇到了一个问题,即使在“成功”连接。在什么时候凭证对象实际填充了 token 列表?

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

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

最佳答案

我通过添加 .setIdTokenRequested(true)

在 GitHub 上的 googlesamples/android-credentials 中找到了解决方案
CredentialRequest credentialRequest = new CredentialRequest.Builder()
                            .setPasswordLoginSupported(true)
                            .setAccountTypes(IdentityProviders.GOOGLE)
                            .setIdTokenRequested(true)
                            .build();

关于Android:Google 登录 - 凭据 token 列表始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34334069/

相关文章:

google-signin - 自动 "Continue as"Google 弹出窗口的文档

android - Google Plus 登录帐户选择对话框问题

Swift:在 View 加载时使用 Google 提示登录

android - GPS消耗多少电池?

java - 如何在android Activity 中获取设备的电话号码

android - 使用 Google Service API 获取最后已知位置

android - Content Provider INSTALL_FAILED_CONFLICTING_PROVIDER(将内容提供程序安装为单独的 apk)

android - Flutter 中的 rxdart 用法未被识别

android - SHA 1 哈希键对所有应用程序都相同吗?

java - 如何检测多次点击并最终在 Android 应用程序中仅触发一次更新