3
votes

I need to integrate Google plus in my application. I referred the Google developers site https://developers.google.com/+/mobile/android/sign-in. I have created a product in Google console and got client Id. I used the PlusClient code given in that site. I doesn't run in my device. In that code they didn't used the client id either. All I want to do is to login in to Google plus, like we used to login in Twitter, Tumblr using a webwiew and access users details.

I used the following code:

package com.googleplus.test;

import java.util.Iterator;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.common.Scopes;
import com.google.android.gms.plus.PlusClient;
import com.google.android.gms.plus.PlusClient.OnPeopleLoadedListener;
import com.google.android.gms.plus.PlusShare;
import com.google.android.gms.plus.model.people.Person;
import com.google.android.gms.plus.model.people.PersonBuffer;


public class GooglePlusTest extends Activity implements android.view.View.OnClickListener,
ConnectionCallbacks, OnConnectionFailedListener, OnPeopleLoadedListener {
     private static final String TAG = "ExampleActivity";
        private static final int REQUEST_CODE_RESOLVE_ERR = 9000;

        private ProgressDialog mConnectionProgressDialog;
        private PlusClient mPlusClient;
        private ConnectionResult mConnectionResult;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            mPlusClient = new PlusClient.Builder(this, this, this).setScopes(Scopes.PLUS_LOGIN, Scopes.PLUS_PROFILE)
                    .setVisibleActivities("http://schemas.google.com/AddActivity").build();

            // Progress bar to be displayed if the connection failure is not
            // resolved.
            mConnectionProgressDialog = new ProgressDialog(this);
            mConnectionProgressDialog.setMessage("Signing in...");
          //  Button shareButton = (Button) findViewById(R.id.share_button);

//shareButton.setOnClickListener(this);
        }

        @Override
        protected void onStart() {
            super.onStart();
            mPlusClient.connect();
        }

        @Override
        protected void onStop() {
            super.onStop();
            mPlusClient.disconnect();
        }

        @Override
        public void onConnectionFailed(ConnectionResult result) {
            if (result.hasResolution()) {
                try {
                    result.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
                } catch (SendIntentException e) {
                    mPlusClient.connect();
                }
            }
            // Save the result and resolve the connection failure upon a user click.
            mConnectionResult = result;
        }

        @Override
        protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
            if (requestCode == REQUEST_CODE_RESOLVE_ERR && responseCode == RESULT_OK) {
                mConnectionResult = null;
                mPlusClient.connect();
            }
        }

        @Override
        public void onConnected(Bundle connectionHint) {
             String accountName = mPlusClient.getAccountName();
//         mPlusClient.loadPerson(this, "104242357859551899867");
     //
         //   mPlusClient.loadPeople(GooglePlusTest.this, Person.Collection.VISIBLE);
            Intent shareIntent = new PlusShare.Builder(this)
              .setType("text/plain")
              .setText("Welcome to the Google+ platform.")
              .setContentUrl(Uri.parse("https://developers.google.com/+/"))
              .getIntent();

          startActivityForResult(shareIntent, 0);

             Toast.makeText(this, accountName + " is connected. try to load persons", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onDisconnected() {
            Log.d(TAG, "disconnected");
        }

        @Override
        public void onClick(View view) {
            // TODO Auto-generated method stub
            if (view.getId() == R.id.sign_in_button && !mPlusClient.isConnected()) {
                if (mConnectionResult == null) {
                    mConnectionProgressDialog.show();
                } else {
                    try {
                        mConnectionResult.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
                    } catch (SendIntentException e) {
                        // Try connecting again.
                        mConnectionResult = null;
                        mPlusClient.connect();
                    }
                }
            }
            else if (view.getId() == R.id.share_button ) {
             Intent shareIntent = new PlusShare.Builder(GooglePlusTest.this)
             .setType("text/plain")
             .setText("Welcome to the Google+ platform.")
             .setContentUrl(Uri.parse("https://developers.google.com/+/"))
             .getIntent();

         startActivityForResult(shareIntent, 0);
            }

        }
        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();
                    Log.i("", person.getNickname());
                }
            }
    }
}
1
google plus or google places, check tag . - Charan Pai
its SIGSEGV not SISSEGV :D - Charan Pai
What happens, or doesn't happen, when you try to sign in? Do you see any errors in logcat? Can you be more specific about the error you are seeing? - Lee
It ask to choose a Google account present in my device to sign up for Google+. If no account is present ask me to create one. I used the post code in onConnected method. It get posted. All I want to do is to use my client id and to login to Google+, like we used to Login in Twitter through webview. - Manikandan
I am also facing same problem that @Manikandan you face....if u got the solution plz tell me hoe to do this - RINK

1 Answers

0
votes

Judging from the fact that you were able to make a post, it sounds like you were able to login. If you want to just login and perform post, you could:

  1. Store user account information so you do not need to contact Google each time you need it.
  2. Set a sharedPerference value that the user is authenticated
  3. Provide a way for user to logout (update sharedPerference) and disconnect (Remove information you stored if user disconnects)
  4. In the onConnected, remove the code used to perform post

Intent shareIntent = new PlusShare.Builder(this).setType("text/plain") .setText("Welcome to the Google+ platform.") .setContentUrl(Uri.parse("https://developers.google.com/+/")).getIntent(); startActivityForResult(shareIntent, 0);

and call the activity you want to take the user to after they have logged in

Intent intent = new Intent(getApplicationContext(),YourActivity.class); startActivity(intent);

Hope this helps.