Plus.PeopleApi.getCurrentPerson(mGoogleApiClient)返回null

时间:2015-08-06 08:34:06

标签: android google-plus google-signin

我尝试了所有方法,但仍然无法解决此问题,Plus.PeopleApi.getCurrentPerson(mGoogleApiClient)始终返回null。

这是我的代码:

mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(Plus.API, Plus.PlusOptions.builder().build())
                .addScope(Plus.SCOPE_PLUS_LOGIN)
                .addScope(Plus.SCOPE_PLUS_PROFILE)
                .build();

这是onConnected()

@Override
    public void onConnected(Bundle arg0) {
        mSignInClicked = false;
        Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();

        // Get user's information
        if (login_status == 1) {
            mSignInClicked = false;
            login_status = 0;
            /* This Line is the key */
            Plus.PeopleApi.loadVisible(mGoogleApiClient, null).setResultCallback(LoginFragment.this);

            getProfileInformation();
        } else {
            signOutFromGplus();
        }

    }

这是getProfileInformation():::: / **      *获取用户的信息名称,电子邮件,个人资料照片      * /'

private void getProfileInformation() {
    try {
        String emailAddr = Plus.AccountApi.getAccountName(mGoogleApiClient);
        Toast.makeText(this,
                "Person information is GOT:" + emailAddr, Toast.LENGTH_LONG).show();

        Person signedInUser = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
        Toast.makeText(this,
                "Person information obj:" + signedInUser, Toast.LENGTH_LONG).show();

        if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
            Person currentPerson = Plus.PeopleApi
                    .getCurrentPerson(mGoogleApiClient);
            String personName = currentPerson.getDisplayName();
            String personPhotoUrl = currentPerson.getImage().getUrl();
            String personGooglePlusProfile = currentPerson.getUrl();
            String email = Plus.AccountApi.getAccountName(mGoogleApiClient);

            Log.e("", "Name: " + personName + ", plusProfile: "
                    + personGooglePlusProfile + ", email: " + email
                    + ", Image: " + personPhotoUrl);
            Toast.makeText(this,
                    "Person information is GOT:" + email, Toast.LENGTH_LONG).show();

            // by default the profile url gives 50x50 px image only
            // we can replace the value with whatever dimension we want by
            // replacing sz=X

        } else {

            signOutFromGplus();
            Toast.makeText(this,
                    "Person information is null", Toast.LENGTH_LONG).show();
        }
        dialog_google.dismiss();
        ;
    } catch (Exception e) {
        e.printStackTrace();
    }
}

我的Android Manifest。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="learn.english.from.hinkhoj" >

    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity
            android:name=".activity.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".activity.LoginFragment"
            android:label="@string/title_activity_login_fragment" >
        </activity>
    </application>

</manifest>

2 个答案:

答案 0 :(得分:3)

1)尝试将以下简单代码添加到回调中:

@Override
public void onConnected(Bundle bundle) {
    Person me = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
    if (me != null) {
        Person.Name name = me.getName();
        String given = name.getGivenName();
        String family = name.getFamilyName();

        Log.d(TAG, "Given: " + given + ", Family: " + family);
    }
}

2)将两个 SHA1签名添加到Google Developers Console / Credentials - 一个从真实密钥库中提取,另一个用于~/.android/debug.keystore(在Mac和Linux上;在Windows上搜索类似路径):

keytool -exportcert -keystore ~/.android/debug.keystore -list -v

3)在Google Developers Console / APIs中启用“Google+ API”和“Google Play Android Developer API”:

console

4)在调试之前运行命令:

adb shell setprop log.tag.GooglePlusPlatform VERBOSE
adb logcat

5)如果什么都没有帮助 - 等几个小时。昨天晚上我遇到了一个应用程序问题(Plus.PeopleApi.getCurrentPerson()无论我尝试了什么都返回null)。今天早上谷歌的爪牙已经修好了。

答案 1 :(得分:0)

您的应用和Google控制台之间的SHA1是否相同? 检查此帖子Android Google Plus getCurrentPerson returns null