当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《Plus.PeopleApi.getCurrentPerson在Play服务8.4中已弃用.如何使用GoogleSignInApi获取用户的名字,姓氏和性别?》经验,为你挑选了2个好方法。

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

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



1> Rahul Sainan..:

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

我自己找到了解决方案,所以如果其他人遇到同样的问题,我会在这里发布.

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


Plus.PeopleApi已被弃用.请参阅以下弃用说明:https://developers.google.com/+/mobile/android/api-deprecation.如果您想获取除第一个/最后一个/显示名称,电子邮件和个人资料图片网址(已由GoogleSignInAccount提供)以外的个人资料信息,请使用新的People REST API.请参阅下面的答案中的代码示例.谢谢!

2> Isabella Che..:

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'

然后写下登录代码,

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

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

推荐阅读
mobiledu2402851203
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有