我正在尝试使用 Play Services 8.3 中引入的新 Google Sign In API 获取用户的个人资料。除了显示名称、电子邮件和 ID 之外,我还需要用户的性别。
Plus.PeopleApi.getCurrentPerson()
根据 play services 8.3 已弃用,并且即使如此也为我返回 null
mGoogleApiClient.hasConnectedApi(Plus.API)
返回真。
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+ 个人资料数据(性别、姓氏、名字等)?
Plus.API 已被弃用。请参阅下面的弃用说明:https://developers.google.com/+/mobile/android/api-deprecation https://developers.google.com/+/mobile/android/api-deprecation
如果您需要名字/姓氏/显示名称、电子邮件和个人资料图片网址(已由登录API https://developers.google.com/identity/sign-in/android/people),请使用新的人员API https://developers.google.com/people/.
在 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
使用 People 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();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)