Play服务8.4中不推荐使用Plus.PeopleApi.getCurrentPerson。如何使用GoogleSignInApi获取用户的名字,姓氏和性别? [英] Plus.PeopleApi.getCurrentPerson deprecated in Play services 8.4. How to get user's first name, last name and gender using GoogleSignInApi?

查看:144
本文介绍了Play服务8.4中不推荐使用Plus.PeopleApi.getCurrentPerson。如何使用GoogleSignInApi获取用户的名字,姓氏和性别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Play服务8.4,方法 getCurrentPerson 已弃用,我使用PeopleApi获取用户的名字,姓氏和性别。

With Play services 8.4, the method getCurrentPerson is deprecated and I was using the PeopleApi to get user's first name, last name and gender.

任何人都可以告诉我如何使用另一种方法获取登录用户的信息吗?

Can anyone tell me how to get the signed in user's info using another method?

推荐答案

Google登录API 已经可以为您提供与第一/最后/显示名称,电子邮件和个人资料图片网址。如果您需要其他个人资料信息(如性别),请将其与新的人员API 一起使用

Google Sign-In API can already provide you with first / last / display name, email and profile picture url. If you need other profile information like gender, use it in conjunction with new People API

// Add dependencies
compile 'com.google.api-client:google-api-client:1.22.0'
compile 'com.google.api-client:google-api-client-android:1.22.0'
compile 'com.google.apis:google-api-services-people:v1-rev4-1.22.0'

然后写入登录码,

Then write sign-in code,

// Make sure your GoogleSignInOptions request profile & email
GoogleSignInOptions gso =
        new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build();
// Follow official doc to sign-in.
// https://developers.google.com/identity/sign-in/android/sign-in

处理登录结果时:

When handling sign-in result:

GoogleSignInResult result =
        Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
    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();
}

使用People Api检索详细的个人信息。

Use People Api to retrieve detailed person info.

/** Global instance of the HTTP transport. */
private static HttpTransport HTTP_TRANSPORT = AndroidHttp.newCompatibleTransport();
/** Global instance of the JSON factory. */
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

// On worker thread
GoogleAccountCredential credential =
         GoogleAccountCredential.usingOAuth2(MainActivity.this, Scopes.PROFILE);
credential.setSelectedAccount(new Account(personEmail, "com.google"));
People service = new People.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
                .setApplicationName(APPLICATION_NAME /* whatever you like */) 
                .build();
// All the person details
Person meProfile = service.people().get("people/me").execute();
// e.g. Gender
List<Gender> genders = meProfile.getGenders();
String gender = null;
if (genders != null && genders.size() > 0) {
    gender = genders.get(0).getValue();
}

查看 JavaDoc 查看您可以获得的其他个人资料信息。

Take a look at JavaDoc to see what other profile information you can get.

这篇关于Play服务8.4中不推荐使用Plus.PeopleApi.getCurrentPerson。如何使用GoogleSignInApi获取用户的名字,姓氏和性别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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