Kotsu
Kotsu

Reputation: 131

Plus.PeopleApi.getCurrentPerson() returns null and no more

i searched alot but still i don't get solution. Plus.PeopleApi.getCurrentPerson() allways returns null. I got my debug SHA1 and release SHA1. Both of them added to console as OAuth 2.0 client IDs with proper package name. (sadly, i cant post images) Here is my 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();
}

Pretty simple. I call it from the Fragment's onCreateView(). Then i'm trying to get the current person:

@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();
    }

And there is allways null. mGoogleApiClient work's fine, it connects, and it is not null, but still. In console->Apis Google+ API enabled, but there is no any requests in "Usage" tab.

I have tryed all solutions i could find and nothing helps. Im running the app with geny emulator. Please, help me, give me any idea.

Added: Here is my almost full Fragment class:

    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();
    }
}

And 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">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
</manifest>

Sorry for some edit bugs.

Upvotes: 2

Views: 3395

Answers (3)

Kotsu
Kotsu

Reputation: 131

I found the solution. Its was not about fragments and SHA1 keys. It was about package name. When i created the project, IDE made the package directory like "somepackage.app", thats wasn't what i needed and i changed it for "somepackage". Everything were working fine, except google api. Gradle saved in its configuration file the "applicationId somepackage.app", and i believe it overrides manifest's package name. No errors, no connects, nothing. I spend for that about 15 hours, i am stupid i believe.

Upvotes: 5

gilsaints88
gilsaints88

Reputation: 1160

Also, on top of @Kotsu's excellent tip on making sure your package name is correct. Also make sure that it is also correct at 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

This is where you have the google-services.json file is generated for you. This is where you specify your AppName and package name. Hope this helps someone as well as it helped me when I also got a null returned from getCurrentPerson.

Upvotes: 0

Michele Lacorte
Michele Lacorte

Reputation: 5363

Add this on top of onCreateView()

private GoogleApiClient mGoogleApiClient;

In onCreateView()

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

In onConnected()

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

And add this method

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();
    }
}

don't forget to implements

ConnectionCallbacks, OnConnectionFailedListener

Add this meta data inside your Manifest.xml under tag 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>

Upvotes: 0

Related Questions