使用Google + / PlusClient获取人员信息

时间:2014-09-18 02:05:27

标签: android google-plus

我试图获取Google+用户的基本信息,以便在我的应用中登录。

我可以连接并获取帐户名称:

String email = mPlusClient.getAccountName(); // This works, gives me the email

然后我尝试获取更多信息:

        mPlusClient.loadVisiblePeople(new OnPeopleLoadedListener() {

        @Override
        public void onPeopleLoaded(ConnectionResult status,
                PersonBuffer personBuffer, String nextPageToken) {

            Log.i("", "persons loaded result = " + status.toString()
                    + ", personsCount = " + personBuffer.getCount()
                    + ", token = " + nextPageToken);
            if (status.isSuccess()) {
                Iterator<Person> itP = personBuffer.iterator();
                while (itP.hasNext()) {
                    Person person = itP.next();
                    Toast.makeText(getApplicationContext(), person.getNickname(), 5).show();
                }
            }
        }
    }, null);

我可以获得朋友信息

persons loaded result = ConnectionResult{statusCode=SUCCESS, resolution=null}, personsCount = 15, token = null

但如果我无法找到如何获取个人信息......

当我尝试:

    Person p = mPlusClient.getCurrentPerson();

p为null,

当我尝试:

mPlusClient.loadPerson(this, "me");

无法识别loadPerson

所以我不知道怎么做......

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:4)

您可以尝试下面的代码来获取当前用户的个人资料信息:

mGoogleApiClient = new GoogleApiClient.Builder(MainActivity.this)
                            .addConnectionCallbacks(this)
                            .addOnConnectionFailedListener(this)
                            .addApi(Plus.API)
                            .addScope(Plus.SCOPE_PLUS_LOGIN)
                            .build();

if(mGoogleApiClient != null && mGoogleApiClient.isConnected()){
                if(Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null){
                    Person mCurrentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
                    String displayName = mCurrentPerson.getDisplayName();
                    int mUserIdOfCurrentUser = mCurrentPerson.getId();
                    Log.d(Constants.LOG_TAG_LOAD_VISIBLE_PEOPLE, "DisplayName: "+displayName);
                    Log.d(Constants.LOG_TAG_LOAD_VISIBLE_PEOPLE,"Id: "+ mCurrentPerson.getId());
                    Log.d(Constants.LOG_TAG_LOAD_VISIBLE_PEOPLE,"Image: "+mCurrentPerson.getImage().getUrl());
                    Log.d(Constants.LOG_TAG_LOAD_VISIBLE_PEOPLE,"List of Urls: "+mCurrentPerson.getUrls());
                    Log.d(Constants.LOG_TAG_LOAD_VISIBLE_PEOPLE,"Current Url: "+mCurrentPerson.getUrl());
                    Log.d(Constants.LOG_TAG_LOAD_VISIBLE_PEOPLE,"Cover: "+mCurrentPerson.getCover());
                    Log.d(Constants.LOG_TAG_LOAD_VISIBLE_PEOPLE,"Object type: "+mCurrentPerson.getObjectType());
                    Log.d(Constants.LOG_TAG_LOAD_VISIBLE_PEOPLE,"User Id : "+mUserIdOfCurrentUser);
                }

答案 1 :(得分:2)

可能是你的签名不同吗?

可能是,您正在使用CUSTOM.keystore(Release Keystore)SHA1。所以我建议您使用DEBUG SHA1进行谷歌控制台。只需在谷歌控制台上添加DEBUG密钥库SHA1即可。它会像我一样工作