Plus.PeopleApi.getCurrentPerson在Play服务8.4中已弃用。 如何使用GoogleSignInApi获取用户的名字,姓氏和性别?

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

任何人都可以告诉我如何使用其他方法获取登录用户的信息?

Google登录API已经可以为您提供第一个/最后一个/显示名称,电子邮件和个人资料图片url。 如果您需要性别等其他个人资料信息,请将其与新的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' 

然后写入登录代码,

 // 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 

处理登录结果时:

 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检索详细的人员信息。

 /** 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(); // eg Gender List genders = meProfile.getGenders(); String gender = null; if (genders != null && genders.size() > 0) { gender = genders.get(0).getValue(); } 

查看JavaDoc以查看您可以获得的其他配置文件信息。

更新:检查伊莎贝拉的答案。 这个答案使用了弃用的东西。

我自己find了解决方案,所以如果其他人面临同样的问题,我会在这里发布。

虽然我正在寻找使用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); } } 

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() { @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); } } 

您好我find了最新Google Plus登录的替代方法,请使用以下方法:

 GoogleApiClient mGoogleApiClient; private void latestGooglePlus() { GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestProfile().requestEmail().requestScopes(Plus.SCOPE_PLUS_LOGIN, Plus.SCOPE_PLUS_PROFILE, new Scope("https://www.googleapis.com/auth/plus.profile.emails.read")) .build(); mGoogleApiClient = new GoogleApiClient.Builder(this) .enableAutoManage(this, this) .addApi(Auth.GOOGLE_SIGN_IN_API, gso) .addApi(Plus.API) .build(); Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); startActivityForResult(signInIntent, YOURREQUESTCODE); } 

Activity结果中使用以下代码:

 @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Log.e("Activity Res", "" + requestCode); if (requestCode == YOURREQUESTCODE) { GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); if (result.isSuccess()) { GoogleSignInAccount acct = result.getSignInAccount(); acct.getPhotoUrl(); acct.getId(); Log.e(TAG, acct.getDisplayName()); Log.e(TAG, acct.getEmail()); Plus.PeopleApi.load(mGoogleApiClient, acct.getId()).setResultCallback(new ResultCallback() { @Override public void onResult(@NonNull People.LoadPeopleResult loadPeopleResult) { Person person = loadPeopleResult.getPersonBuffer().get(0); Log.d(TAG, (person.getName().getGivenName())); Log.d(TAG, (person.getName().getFamilyName())); Log.d(TAG, (person.getDisplayName())); Log.d(TAG, (person.getGender() + "")); Log.d(TAG, "person.getCover():" + person.getCover().getCoverPhoto().getUrl()); } }); } } } 

最后你的onClick将是:

 @Override public void onClick(View v) { switch (v.getId()) { case R.id.txtGooglePlus: latestGooglePlus(); break; default: break; } } 

首先要做的是在添加Google登录到您的Android应用程序时遵循谷歌定位。

然后你必须将GoogleSignInOptions更改为:

 GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestIdToken(getString(R.string.default_web_client_id)) .requestProfile() .requestEmail() .build(); 

如果您需要添加其他范围,可以这样做:

 GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestIdToken(getString(R.string.default_web_client_id)) .requestScopes(new Scope(Scopes.DRIVE_APPFOLDER)) .requestProfile() .requestEmail() .build(); 

在’onActivityResult’里面’if(result.isSuccess()){‘插入这个:

 new requestUserInfoAsync(this /* Context */, acct).execute(); 

并创建此方法:

 private static class requestUserInfoAsync extends AsyncTask { // 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(); private Context context; private GoogleSignInAccount acct; private String birthdayText; private String addressText; private String cover; public requestUserInfoAsync(Context context, GoogleSignInAccount acct) { this.context = context; this.acct = acct; } @Override protected Void doInBackground(Void... params) { // On worker thread GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2( context, Collections.singleton(Scopes.PROFILE) ); credential.setSelectedAccount(new Account(acct.getEmail(), "com.google")); People service = new People.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential) .setApplicationName(context.getString(R.string.app_name) /* whatever you like */) .build(); // All the person details try { Person meProfile = service.people().get("people/me").execute(); List birthdays = meProfile.getBirthdays(); if (birthdays != null && birthdays.size() > 0) { Birthday birthday = birthdays.get(0); // DateFormat.getDateInstance(DateFormat.FULL).format(birthdayDate) birthdayText = ""; try { if (birthday.getDate().getYear() != null) { birthdayText += birthday.getDate().getYear() + " "; } birthdayText += birthday.getDate().getMonth() + " " + birthday.getDate().getDay(); } catch (Exception e) { e.printStackTrace(); } } List
addresses = meProfile.getAddresses(); if (addresses != null && addresses.size() > 0) { Address address = addresses.get(0); addressText = address.getFormattedValue(); } List coverPhotos = meProfile.getCoverPhotos(); if (coverPhotos != null && coverPhotos.size() > 0) { CoverPhoto coverPhoto = coverPhotos.get(0); cover = coverPhoto.getUrl(); } } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); Log.i("TagTag", "birthday: " + birthdayText); Log.i("TagTag", "address: " + addressText); Log.i("TagTag", "cover: " + cover); } }

使用此方法,您可以使用“Person meProfile”中的方法获取其他信息,但您只能获取用户的公共信息,否则将为null。

只是为了补充哈代上面的回答,这引导我朝着正确的方向前进。

我最终使用了两次对GoogleApiClient的调用,因为我无法得到Hardy上面的工作。

我的第一个电话是GoogleSignInApi

 GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestProfile() .requestEmail() .requestIdToken(MY_GOOGLE_SERVER_CLIENT_ID) .build(); mGoogleApiClient = new GoogleApiClient.Builder(getActivity()) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(Auth.GOOGLE_SIGN_IN_API, gso) .build(); Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); startActivityForResult(signInIntent, ThinQStepsConstants.REQUEST_CODE_GOOGLE_SIGN_IN); 

然后通过onActivityResult给我第一部分,和Hardy一样。 然而,我再次使用对GoogleApiClient.Builder的调用

 mGoogleApiClientPlus = new GoogleApiClient.Builder(getActivity()) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(Plus.API) .addScope(Plus.SCOPE_PLUS_PROFILE) .build(); mGoogleApiClientPlus.connect(); 

现在我可以通过onConnected回调访问Plus.PeopleApi

 @Override public void onConnected(Bundle connectionHint) { Log.i(TAG, "onConnected"); Plus.PeopleApi.load(mGoogleApiClientPlus, mGoogleId).setResultCallback(new ResultCallback() { @Override public void onResult(@NonNull People.LoadPeopleResult loadPeopleResult) { Person currentPerson = loadPeopleResult.getPersonBuffer().get(0); } }); } 

适当的断开连接和撤销。

您可能会注意到我的代码使用了相同的回调,我需要整理它,但是主体就在那里。