通过 Android 访问 Google 帐户 ID/用户名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2245545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:08:56  来源:igfitidea点击:

Accessing Google Account Id /username via Android

androidgoogle-account

提问by KawBoy

How do you access the user's Google Account Id / username in code? I am building an application that will call a web service to store data and I want to identify the identity of the person submitting the data.

您如何在代码中访问用户的 Google 帐户 ID/用户名?我正在构建一个将调用 Web 服务来存储数据的应用程序,我想确定提交数据的人的身份。

回答by Fabricio Buzeto

I've ran into the same issue and these two links solved for me:

我遇到了同样的问题,这两个链接为我解决了:

The first one is this one: How do I retrieve the logged in Google account on android phones?

第一个是这个:如何在Android手机上检索登录的Google帐户?

Which presents the code for retrieving the accounts associated with the phone. Basically you will need something like this:

其中显示了用于检索与手机关联的帐户的代码。基本上你需要这样的东西:

AccountManager manager = (AccountManager) getSystemService(ACCOUNT_SERVICE);
Account[] list = manager.getAccounts();

And to add the permissions in the AndroidManifest.xml

并在 AndroidManifest.xml 中添加权限

<uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission>
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"></uses-permission>

Additionally, if you are using the Emulator the following link will help you to set it up with an account : Android Emulator - Trouble creating user accounts

此外,如果您使用的是模拟器,以下链接将帮助您使用帐户进行设置:Android 模拟器 -无法创建用户帐户

Basically, it says that you must create an android device based on a API Level and not the SDK Version (like is usually done).

基本上,它表示您必须基于 API 级别而不是 SDK 版本创建一个 android 设备(就像通常所做的那样)。

回答by Rahul Devganiya

This Method to get Google Username:

获取谷歌用户名的方法:

 public String getUsername() {
    AccountManager manager = AccountManager.get(this);
    Account[] accounts = manager.getAccountsByType("com.google");
    List<String> possibleEmails = new LinkedList<String>();

    for (Account account : accounts) {
        // TODO: Check possibleEmail against an email regex or treat
        // account.name as an email address only for certain account.type
        // values.
        possibleEmails.add(account.name);
    }

    if (!possibleEmails.isEmpty() && possibleEmails.get(0) != null) {
        String email = possibleEmails.get(0);
        String[] parts = email.split("@");
        if (parts.length > 0 && parts[0] != null)
            return parts[0];
        else
            return null;
    } else
        return null;
}

simple this method call ....

简单这个方法调用....

And Get Google User in Gmail id::

并在 Gmail id 中获取 Google 用户 ::

 accounts = AccountManager.get(this).getAccounts();
    Log.e("", "Size: " + accounts.length);
    for (Account account : accounts) {

        String possibleEmail = account.name;
        String type = account.type;

        if (type.equals("com.google")) {
            strGmail = possibleEmail;

            Log.e("", "Emails: " + strGmail);
            break;
        }
    }

After add permission in manifest;

在清单中添加权限后;

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<uses-permission android:name="android.permission.GET_ACCOUNTS" />

回答by shweta jariya

Retrieve profile information for a signed-in user Use the GoogleSignInResult.getSignInAccount method to request profile information for the currently signed in user. You can call the getSignInAccount method after the sign-in intent succeeds.

检索登录用户的个人资料信息 使用 GoogleSignInResult.getSignInAccount 方法请求当前登录用户的个人资料信息。您可以在登录意图成功后调用 getSignInAccount 方法。

GoogleSignInResult result = 
Auth.GoogleSignInApi.getSignInResultFromIntent(data);
GoogleSignInAccount acct = result.getSignInAccount();
String personName = acct.getDisplayName();
String personGivenName = acct.getGivenName();
String personFamilyName = acct.getFamilyName();
String personEmail = acct.getEmail();
String personId = acct.getId();
Uri personPhoto = acct.getPhotoUrl();

回答by Erik Johansson

Used these lines:

使用了这些行:

AccountManager manager = AccountManager.get(this);
Account[] accounts = manager.getAccountsByType("com.google");

the length of array accounts is always 0.

数组帐户的长度始终为 0。

回答by Dixit Panchal

if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
  Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
  String userid=currentPerson.getId(); //BY THIS CODE YOU CAN GET CURRENT LOGIN USER ID
}

回答by Balaji

There is a sample from google, which lists the existing google accounts and generates an access token upon selection , you can send that access token to server to retrieve the related details from it to identify the user.

有一个来自 google 的示例,它列出了现有的 google 帐户并在选择时生成访问令牌,您可以将该访问令牌发送到服务器以从中检索相关详细信息以识别用户。

You can also get the email id from access token , for that you need to modify the SCOPE

您还可以从访问令牌中获取电子邮件 ID,为此您需要修改 SCOPE

Please go through My Post

请通过我的帖子

回答by Ahmadullah Saikat

String name = android.os.Build.USER;

if (!TextUtils.isEmpty(name)) {
    nameEdit.setText(name);
}