Get Google account domain in Android

旧时模样 提交于 2019-12-22 12:39:34

问题


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.

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).

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.


回答1:


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
    }
}
}

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



来源:https://stackoverflow.com/questions/30470659/get-google-account-domain-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!