Android 在新的google登录播放服务8.3中获取个人详细信息

Android 在新的google登录播放服务8.3中获取个人详细信息,android,google-play-services,google-api-client,google-signin,google-plus-signin,Android,Google Play Services,Google Api Client,Google Signin,Google Plus Signin,我正在尝试使用PlayServices8.3中引入的新Google登录API获取用户的个人资料。除了显示姓名,电子邮件和Id,我还需要用户的性别 Plus.PeopleApi.getCurrentPerson() 已根据play services 8.3弃用,即使 mGoogleApiClient.hasConnectedApi(Plus.API) 返回true GoogleSignInAccount.getGrantedScopes 返回 https://www.googleapi

我正在尝试使用PlayServices8.3中引入的新Google登录API获取用户的个人资料。除了显示姓名,电子邮件和Id,我还需要用户的性别

Plus.PeopleApi.getCurrentPerson() 
已根据play services 8.3弃用,即使

mGoogleApiClient.hasConnectedApi(Plus.API) 
返回true

GoogleSignInAccount.getGrantedScopes 
返回

https://www.googleapis.com/auth/plus.me
https://www.googleapis.com/auth/plus.login
profile
email
openid
Google开发者控制台没有显示Google+API上的任何点击。我已将正确的google-services.json文件放在应用程序的app/文件夹中。我甚至通过编程生成了SHA1指纹,以验证我是否使用了正确的密钥库


如何使用新的登录API获取google+个人资料数据(性别、姓氏、姓名等)?

更新:根据@Isabella Chen下面的评论,对于不想使用标记为不推荐的
getCurrentPerson
的用户,您可以开始使用,您也可以在下面的S.O问题中看到我的另一个答案:


在国际海事组织,您可以参考以下代码:

        // [START configure_signin]
        // Configure sign-in to request the user's ID, email address, and basic
        // profile. ID and basic profile are included in DEFAULT_SIGN_IN.
        // FOR PROFILE PICTURE:
        // Ref: https://developers.google.com/android/reference/com/google/android/gms/auth/api/signin/GoogleSignInAccount.html#getPhotoUrl%28%29
        // getPhotoUrl(): Gets the photo url of the signed in user.
        // Only non-null if requestProfile() is configured and user does have a Google+ profile picture.
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestScopes(new Scope(Scopes.PROFILE))
                .requestScopes(new Scope(Scopes.PLUS_LOGIN))
                .requestProfile()
                .requestEmail()
                .build();
        // [END configure_signin]

        // [START build_client]
        // Build a GoogleApiClient with access to the Google Sign-In API and the
        // options specified by gso.
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .addApi(Plus.API)
                .build();
        // [END build_client]
然后:

日志信息:

11-20 09:06:35.431 31289-31289/com.example.googlesignindemo I/GoogleSignIn: Gender: 0

希望这有帮助

Plus.API已被弃用。请参阅下面的弃用说明:

如果您需要第一个/最后一个/显示名称、电子邮件和配置文件图片url(已由提供)以外的配置文件信息,请使用新的

在Android上,您可以执行以下操作:

// 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'
然后编写登录代码

// 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
使用人员Api检索详细的人员信息

/** 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(googleSignInAccount.getEmail(), "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();
}
/**HTTP传输的全局实例*/
私有静态HttpTransport HTTP_TRANSPORT=AndroidHttp.newcomppatibletransport();
/**JSON工厂的全局实例*/
私有静态最终JsonFactory JSON_FACTORY=JacksonFactory.getDefaultInstance();
//在工作线程上
GoogleAccountCredential凭证=
GoogleAccountCredential.usingAuth2(MainActivity.this、Scopes.PROFILE);
credential.setSelectedAccount(
新帐户(googleSignInAccount.getEmail(),“com.google”);
人员服务=new People.Builder(HTTP_传输、JSON_工厂、凭证)
.setApplicationName(应用程序名称/*任意您喜欢的*/)
.build();
//所有人的详细信息
Person-meProfile=service.people().get(“people/me”).execute();
//例如性别
List genders=meProfile.getGenders();
字符串性别=空;
if(genders!=null&&genders.size()>0){
gender=genders.get(0.getValue();
}

我认为谷歌推出了迄今为止最简单的获取个人资料的方法

GoogleSignInAccount acct = GoogleSignIn.getLastSignedInAccount(getActivity());
if (acct != null) {
  String personName = acct.getDisplayName();
  String personGivenName = acct.getGivenName();
  String personFamilyName = acct.getFamilyName();
  String personEmail = acct.getEmail();
  String personId = acct.getId();
  Uri personPhoto = acct.getPhotoUrl();
}

我比较了整个代码,最后意识到gradle中的应用程序id有一个输入错误:|感谢您的帮助。Plus.API已被弃用。见我贴出的答案。谢谢@感谢BNK向开发者发布了许多有用的答案,帮助他们解决Google API集成问题。是的,我知道答案是去年发布的,当时Plus.API绝对是获取详细配置文件信息的最简单方法。但是,正如您从弃用说明中看到的,Google将停止通过Plus.PeopleApi返回数据,即使是现有的OAuth授权。因此,我们希望开发人员能够使用最新的受欢迎的方法访问人员详细信息,并且在时机成熟时不会受到不推荐的影响。@BNK,Plus.PeopleApi作为一个整体已被不推荐,请参阅Javadoc:。在Sep中,新登录将不会返回circle数据;2017年第一季度,即使是现有的赠款也将收回空数据。再次感谢您对GoogleAPI的一贯支持和对开发者社区的帮助。然而,我们应该让人们不再在Android上使用任何G+API(除了+1/共享小部件等),而是让他们使用最新的替代品,普通人API(目前只有REST可用)。谢谢大家!@BNK,是的,即使你使用8.3。Android API是底层REST API的包装器,REST弃用说明将很快发布!您能告诉我如何自动获取有关谷歌弃用说明的更新信息吗?例如,通过电子邮件订阅?@BNK不幸的是,没有可用的订阅渠道。但通常情况下,谷歌Play services SDK发行说明中会宣布不推荐:。另外,现在Android Studio将提示您进行Google Play services SDK更新。如果您总是更新,您将在不推荐使用的API上看到删除线。然后,您可以参考Javadoc了解更多详细信息。请问如何使用HTTPS代替HTTP?并且应该
GoogleAccountCredential credential=GoogleAccountCredential.usingAuth2(MainActivity.this,Collections.singletonList(Scopes.PROFILE))在新版本中使用?“确保您的谷歌登录请求配置文件和电子邮件”。这意味着GoogleSignionOptions.Builder需要.requestEmail()和.requestProfile(),我猜?!是的,或者GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().build();另见官方文件:
GoogleSignInAccount acct = GoogleSignIn.getLastSignedInAccount(getActivity());
if (acct != null) {
  String personName = acct.getDisplayName();
  String personGivenName = acct.getGivenName();
  String personFamilyName = acct.getFamilyName();
  String personEmail = acct.getEmail();
  String personId = acct.getId();
  Uri personPhoto = acct.getPhotoUrl();
}