Plus.PeopleApi.getCurrentPerson()返回null,仅此而已



我搜索了很多,但仍然没有得到解决方案。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();
    }

总是null。mGoogleApiClient工作很好,它连接,它不是空的,但仍然。在控制台-> API中启用了Google+ API,但在"用法"选项卡中没有任何请求。

我已经尝试了所有我能找到的解决方案,没有任何帮助。我运行的应用程序与根模拟器。请帮帮我,给我一些建议。

添加:这是我几乎完整的片段类:

    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>

对编辑错误表示抱歉

我找到了解决方案。它不是关于片段和SHA1密钥。这是关于包的名字。当我创建项目时,IDE将包目录设置为"somepackage"。App",这不是我需要的,我把它改成了"somepackage"。一切都很好,除了谷歌api。Gradle在它的配置文件中保存了"applicationId somepackage"。App",我相信它会覆盖manifest的包名。没有错误,没有连接,什么都没有。我花了大约15个小时,我相信我很笨。

将此添加到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中的标签application

   <meta-data
      android:name="com.google.android.gms.version"
      android:value="@integer/google_play_services_version" />

Try this manifest

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

此外,在@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

上也是正确的

这里有谷歌服务。Json文件为您生成。这是指定AppName和包名的地方。希望这有助于有人以及它帮助了我,当我也得到了一个null从getCurrentPerson返回。

相关内容

  • 没有找到相关文章

最新更新