Plus.PeopleApi.getCurrentPerson()返回null而不再返回

时间:2015-09-02 15:31:16

标签: android google-api

我搜索了很多但仍然没有得到解决方案。 Plus.PeopleApi.getCurrentPerson()总是返回null。 我得到了调试SHA1并释放了SHA1。它们都作为具有正确包名的OAuth 2.0客户端ID添加到控制台。 (遗憾的是,我无法发布图片) 这是我的GoogleApiClient:

    private GoogleApiClient buildGoogleApiClient() {
    return new GoogleApiClient.Builder(getContext())
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API)
            .addScope(new Scope(Scopes.PROFILE))
            .addScope(new Scope(Scopes.PLUS_LOGIN))
            .build();
}

非常简单。我从Fragment的onCreateView()中调用它。然后我试图找到当前的人:

@Override
public void onConnected(Bundle bundle) {
    Plus.PeopleApi.loadVisible(mGoogleApiClient, null).setResultCallback(this);

    if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
        Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
        String personName = currentPerson.getDisplayName();
        String personGooglePlusProfile = currentPerson.getUrl();
    }

总是空的。 mGoogleApiClient工作正常,它连接,它不是null,但仍然。 在控制台 - >启用了Apis Google+ API,但在" Usage"中没有任何请求。标签

我已经尝试了所有可以找到的解决方案而且没有任何帮助。 我用geny模拟器运行应用程序。 请帮帮我,给我任何想法。

补充:这是我几乎完整的Fragment类:

    public class AuthorizationFragment2 extends Fragment implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        View.OnClickListener,
        ResultCallback {
    private static final String KEY_IS_RESOLVING = "is_resolving";
    private static final String KEY_SHOULD_RESOLVE = "should_resolve";
    /* Request code used to invoke sign in user interactions. */
    protected static final int RC_SIGN_IN = 0;
    /* GP Is there a ConnectionResult resolution in progress? */
    private boolean mIsResolving = false;
    /* GP Should we automatically resolve ConnectionResults when possible? */
    private boolean mShouldResolve = false;
    private GoogleApiClient mGoogleApiClient;
    /* View to display current status (signed-in, signed-out, disconnected, etc) */
    private TextView mStatus;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            mIsResolving = savedInstanceState.getBoolean(KEY_IS_RESOLVING);
            mShouldResolve = savedInstanceState.getBoolean(KEY_SHOULD_RESOLVE);
        }
        mGoogleApiClient = buildGoogleApiClient();
        View view = inflater.inflate(R.layout.authorization_view, container, false);
        // Register onClickListener for gp_login_button
        view.findViewById(R.id.gp_login_button).setOnClickListener(this);
        view.findViewById(R.id.sign_out_button).setOnClickListener(this);
        mStatus = (TextView) view.findViewById(R.id.mStatus);
        return view;
    }
    @Override
    public void onStart() {
        super.onStart();
        mGoogleApiClient.connect();
    }
    @Override
    public void onStop() {
        super.onStop();
        mGoogleApiClient.disconnect();
    }
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean(KEY_IS_RESOLVING, mIsResolving);
        outState.putBoolean(KEY_SHOULD_RESOLVE, mShouldResolve);
    }
    @Override
    public void onConnected(Bundle bundle) {
        Plus.PeopleApi.loadVisible(mGoogleApiClient, null).setResultCallback(this);
        // It is allways null
        if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
            Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
            // Retrieve Person's information
        }
        updateUI(true);
    }
    @Override
    public void onConnectionSuspended(int i) {
        Log.d(MainActivity.LOG_TAG, "onCOnnectionSuspended()");
    }
    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        if(!mIsResolving && mShouldResolve) {
            if (connectionResult.hasResolution()) {
                try {
                    // It calls the Activity's onActivityResult()
                    connectionResult.startResolutionForResult(getActivity(), RC_SIGN_IN);
                    mIsResolving = true;
                } catch (IntentSender.SendIntentException e) {
                    mIsResolving = false;
                    mGoogleApiClient.connect();
                }
            } else {
                // Could not resolve the connection result, show the user an
                // error dialog.
                showErrorDialog(connectionResult);
            }
        } else {
            // Show the signed-out UI
            updateUI(false);
        }
    }
    private GoogleApiClient buildGoogleApiClient() {
        return new GoogleApiClient.Builder(getActivity().getApplicationContext())
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(Plus.API)
                .addScope(Plus.SCOPE_PLUS_PROFILE)
                .addScope(Plus.SCOPE_PLUS_LOGIN)
                .build();
    }
    /**
     * This called only from Activity's onActivityResult() with requestCode == RC_SIGN_IN
     */
    protected void connect(int resultCode) {
        // If the error resolution was not successful we should not resolve further.
        if (resultCode != Activity.RESULT_OK) {
            mShouldResolve = false;
        }
        mIsResolving = false;
        mGoogleApiClient.connect();
    }
}

并表明:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="somepackage" >
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    <application
        android:allowBackup="true"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
</manifest>

抱歉有些编辑错误。

3 个答案:

答案 0 :(得分:5)

我找到了解决方案。它不是关于片段和SHA1键。这是关于包名。 当我创建项目时,IDE将包目录设置为&#34; somepackage.app&#34;,这不是我需要的东西,我为#34; somepackage&#34;更改了它。一切都很好,除了谷歌api。 Gradle在其配置文件中保存了&#34; applicationId somepackage.app&#34;,我相信它会覆盖清单的包名。 没有错误,没有连接,没有。我花了大约15个小时,我相信我是愚蠢的。

答案 1 :(得分:0)

在onCreateView()

之上添加它
private GoogleApiClient mGoogleApiClient;

在onCreateView()

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

在onConnected()

@Override
public void onConnected(Bundle bundle) {
      getProfileInformation();
    }

并添加此方法

public void getProfileInformation() {
    try {
        if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
            Person currentPerson = Plus.PeopleApi
                    .getCurrentPerson(mGoogleApiClient);
            String name = currentPerson.getDisplayName();
            String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
        } else {
            Toast.makeText(getActivity(),
                    "Person information is null", Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

不要忘记实施

ConnectionCallbacks,OnConnectionFailedListener

在标记应用程序

下的Manifest.xml中添加此元数据
   <meta-data
      android:name="com.google.android.gms.version"
      android:value="@integer/google_play_services_version" />

试试这个清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="somepackage" >
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    <application
        android:allowBackup="true"
        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=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
</manifest>

答案 2 :(得分:0)

此外,除了@Kotsu关于确保您的包名称正确的优秀提示之外。同时确保https://developers.google.com/mobile/add?platform=android&cntapi=signin&cnturl=https:%2F%2Fdevelopers.google.com%2Fidentity%2Fsign-in%2Fandroid%2Fsign-in%3Fconfigured%3Dtrue&cntlbl=Continue%20Adding%20Sign-In

也正确

这是您为自己生成google-services.json文件的地方。您可以在此处指定AppName和包名称。希望这可以帮助我,当我从getCurrentPerson返回null时,它也帮助了我。