在Android中获取Google帐户域 [英] Get Google account domain in Android

查看:114
本文介绍了在Android中获取Google帐户域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android应用中使用 Google+登录。我如何获得经过身份验证的用户帐户的域名?它不是com.google.android.gms.plus.model.people.Person模型中的一个属性。

I am using Google+ Sign-In in my Android app. How can I get the domain of the authenticated user's account? It is not a property on the com.google.android.gms.plus.model.people.Person model.

具体而言,我只希望来自一家公司的人员能够使用他们的工作Google帐户访问应用程序(公司中的每个人都有一个)。

Specifically I only want people from one company to be able to access the app using their work Google accounts (everyone in the company has one).

我当然可以使用 Plus.AccountApi.getAccountName(mGoogleApiClient); 来检索电子邮件地址,并且可以检查域名的电子邮件地址,但似乎讨厌。

I can of course retrieve the email address using Plus.AccountApi.getAccountName(mGoogleApiClient); and could check the domain of the email address, but that seems nasty.

推荐答案

您正试图在错误的时间在帐户域中筛选流量。您应该在用户登录前进行检查。

You are trying to filter on the account domain at the wrong time in the flow. You should do the check before the user signs in.

为此,请检索系统上的所有帐户,并检查是否符合条件并且只有在找到后才能登录一个适合的帐户。

For this, retrieve all accounts on the system and check for an eligible one and only log-in once you have found a fitting account.

一个简单的活动来获取所有帐户并过滤它们可能看起来像这样:

A simple activity to get all accounts and filter on them could look like this:

public class CheckAccounts extends AppCompatActivity {

private static final int MY_PERMISSIONS_REQUEST_GET_ACCOUNTS = 8;
final String TAG = this.getClass().toString();

private void getPermissions() {
    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(this,
            Manifest.permission.GET_ACCOUNTS)) {

        // Show an explanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

    } else {

        // No explanation needed, we can request the permission.

        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.GET_ACCOUNTS},
                MY_PERMISSIONS_REQUEST_GET_ACCOUNTS);

        // MY_PERMISSIONS_REQUEST_GET_ACCOUNTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}

private void getAccounts() {

    // Here, thisActivity is the current activity
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.GET_ACCOUNTS)
            != PackageManager.PERMISSION_GRANTED) {

        getPermissions();
    }

    AccountManager accountManager = AccountManager.get(this);

    Account[] accounts = accountManager.getAccountsByType("com.google");

    for (Account a : accounts){
        String accountName = a.name;
        String domain = accountName.substring(accountName.indexOf("@") + 1, accountName.length());
        Log.d(TAG, "account domain: " + domain);
    }

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_check_accounts);
    getAccounts();

}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_GET_ACCOUNTS: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                getAccounts();
                // permission was granted, yay! Do the
                // contacts-related task you need to do.

            } else {
                Log.d(TAG, "didn't get perms");
                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}
}

一旦找到合适的帐户,您可以使用 GoogleSignInOptions.Builder setAccountName 使用此特定帐户登录。如果您在设备上找不到合适的帐户,可以直接通知用户,而不是先登录自己的帐户。

Once you have found a suitable account you can use GoogleSignInOptions.Builder setAccountName to log-in with this specific account. If you cannot find a fitting account configured on the device, you can simply notify the user rather than logging in to his or her account first.

问候,
米尔科

Regards, Mirko

这篇关于在Android中获取Google帐户域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆