获取用户邮箱 firebase auth

Get the user email firebase auth

我正在尝试做这样的事情

我在 shopify 应用程序和 discord 应用程序等几个应用程序中发现了这个弹出对话框,一旦你打开注册或登录页面对话框就会弹出所有可能的电子邮件。

现在我的问题是如何在不征求用户联系许可的情况下实现这样的目标。

我当前的代码是:

private void addAdapterToViews() {
    Account[] accounts = AccountManager.get(getActivity()).getAccounts();
    Set<String> emailSet = new HashSet<String>();
    for (Account account : accounts) {
        if (EMAIL_PATTERN.matcher(account.name).matches()) {
            emailSet.add(account.name);
        }
    }
    binding.autoemail.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_dropdown_item_1line, new ArrayList<String>(emailSet)));
}

private boolean mayRequestContacts() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        return true;
    }
    if (getActivity().checkSelfPermission(READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
        return true;
    }
    if (shouldShowRequestPermissionRationale(READ_CONTACTS)) {
        requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS);
        Snackbar.make(binding.autoemail, R.string.permission_rationale, Snackbar.LENGTH_INDEFINITE)
                .setAction(android.R.string.ok, new View.OnClickListener() {
                    @Override
                    @TargetApi(Build.VERSION_CODES.M)
                    public void onClick(View v) {
                        requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS);
                    }
                });
    } else {
        requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS);
    }
    return false;
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    if (requestCode == REQUEST_READ_CONTACTS) {
        if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            addAdapterToViews();
        }
    }
}

一旦用户尝试在电子邮件中键入任何字母 edittext 如果获得许可,此代码对我来说工作正常。

没有运行时权限,无法访问用户机密信息(如电子邮件 ID)。

也许这不是你要找的,但我真的建议你看看Authentication with Firebase and the helper library FirebaseUI。他们已经实现了所有样板文件,而且配置、定制和设置真的很容易。

到目前为止,我发现使用 google 服务在未经任何许可的情况下使用以下代码获取所有用户电子邮件:

private static final int RC_SIGN_IN = 100;

OnCreate() 或按钮内单击 :

AuthUI.SignInIntentBuilder builder = AuthUI.getInstance().createSignInIntentBuilder();

if (getSelectedTosUrl() != null && getSelectedPrivacyPolicyUrl() != null) {
    builder.setTosAndPrivacyPolicyUrls(getSelectedTosUrl(), getSelectedPrivacyPolicyUrl());
}
builder.build();
startActivityForResult(builder.build(), RC_SIGN_IN);

并获得结果

 @Nullable
    private String getSelectedPrivacyPolicyUrl() {

        return null;
    }

    @Nullable
    private String getSelectedTosUrl() {

        return null;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RC_SIGN_IN) {
            handleSignInResponse(resultCode, data);
        }
    }

    private void handleSignInResponse(int resultCode, @Nullable Intent data) {
        IdpResponse response = IdpResponse.fromResultIntent(data);

        // Successfully signed in
        if (resultCode == RESULT_OK) {
//            startSignedInActivity(response);
//            finish();
            Log.e(TAG, String.valueOf(response));

        } else {
            // Sign in failed
            if (response == null) {
                // User pressed back button
//                showSnackbar(R.string.sign_in_cancelled);
                Log.e(TAG, "Sign-in error: failed");

                return;
            }

            if (response.getError().getErrorCode() == ErrorCodes.NO_NETWORK) {
//                showSnackbar(R.string.no_internet_connection);
                Log.e(TAG, "Sign-in error: no net");
                return;
            }

//            showSnackbar(R.string.unknown_error);
            Log.e(TAG, "Sign-in error: ", response.getError());
        }
    }

但我的问题是用户电子邮件对话框在预制登录活动中打开,但我想使用我自己的。 有小费吗 ?这是来源 google library

用于检索登录提示的凭据API,例如用户名和电子邮件地址。

在 Android 6.0 (Marshmallow) 及更高版本上,您的应用无需请求任何设备或运行时权限即可使用凭据 API

检索登录提示
HintRequest hintRequest = new HintRequest.Builder()
        .setHintPickerConfig(new CredentialPickerConfig.Builder()
                .setShowCancelButton(true)
                .build())
        .setEmailAddressIdentifierSupported(true)
        .setAccountTypes(IdentityProviders.GOOGLE)
        .build();

PendingIntent intent = mCredentialsClient.getHintPickerIntent(hintRequest);
try {
    startIntentSenderForResult(intent.getIntentSender(), RC_HINT, null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
    Log.e(TAG, "Could not start hint picker Intent", e);
}

HintRequest Builder Google Documentation