Rah*_*ani 6 android google-play-services google-plus-signin google-signin
使用Play服务8.4,方法getCurrentPerson已弃用,我使用PeopleApi来获取用户的名字,姓氏和性别.
任何人都可以告诉我如何使用其他方法获取登录用户的信息?
Rah*_*ani 11
更新:检查伊莎贝拉的答案.这个答案使用了弃用的东西.
我自己找到了解决方案,所以如果其他人遇到同样的问题,我会在这里发布.
虽然我正在寻找使用GoogleSignInApi获取用户信息的解决方案,但我找不到,我认为我们需要使用Plus Api来获取性别等信息.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
Run Code Online (Sandbox Code Playgroud)
HandleSignInResult
private void handleSignInResult(GoogleSignInResult result)
{
Log.d(TAG, "handleSignInResult:" + result.isSuccess());
if (result.isSuccess())
{
GoogleSignInAccount acct = result.getSignInAccount();
Toast.makeText(getApplicationContext(),""+acct.getDisplayName(),Toast.LENGTH_LONG).show();
Plus.PeopleApi.load(mGoogleApiClient, acct.getId()).setResultCallback(new ResultCallback<People.LoadPeopleResult>() {
@Override
public void onResult(@NonNull People.LoadPeopleResult loadPeopleResult) {
Person person = loadPeopleResult.getPersonBuffer().get(0);
Log.d(TAG,"Person loaded");
Log.d(TAG,"GivenName "+person.getName().getGivenName());
Log.d(TAG,"FamilyName "+person.getName().getFamilyName());
Log.d(TAG,("DisplayName "+person.getDisplayName()));
Log.d(TAG,"Gender "+person.getGender());
Log.d(TAG,"Url "+person.getUrl());
Log.d(TAG,"CurrentLocation "+person.getCurrentLocation());
Log.d(TAG,"AboutMe "+person.getAboutMe());
Log.d(TAG,"Birthday "+person.getBirthday());
Log.d(TAG,"Image "+person.getImage());
}
});
//mStatusTextView.setText(getString(R.string.signed_in_fmt, acct.getDisplayName()));
//updateUI(true);
} else {
//updateUI(false);
}
}
Run Code Online (Sandbox Code Playgroud)
Google登录API已经可以为您提供第一个/最后一个/显示名称,电子邮件和个人资料图片网址.如果您需要性别等其他个人资料信息,请将其与新的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'
Run Code Online (Sandbox Code Playgroud)
然后写下登录代码,
// 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
Run Code Online (Sandbox Code Playgroud)
处理登录结果时:
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();
}
Run Code Online (Sandbox Code Playgroud)
使用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(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();
}
Run Code Online (Sandbox Code Playgroud)
查看JavaDoc以查看您可以获得的其他配置文件信息.
| 归档时间: |
|
| 查看次数: |
7338 次 |
| 最近记录: |