Integrating Google Plus Sign In into Android Application 2


In this tutorial, you will learn how to Integrate Google Plus API to Sign In User in your Android Application. Google Plus sign-in lets users sign in to your Android app with their existing Google account and get their profile information like name, email, profile pic and other details. So lets begin.

Prerequisites:

Registering your application

  1. Open the Google Cloud Console.
  2. If you haven’t created the API project yet, click CREATE PROJECT. Give the name of the project and click Create.
    gcm_demo3
    g+ create project
  3. In the sidebar on the left, click APIs and auth. In the displayed list of APIs, Enable Google+ API service.
    g+ API Enable
  4. In the sidebar on the left, click APIs and auth and Credentials. On OAuth content screen provide the project name.
    g+ client ID
  5. Select OAuth 2.0 Client ID under Add Credentials to create a new Client ID.  Now select Android Application.
  6. Enter the details like Name for this Client ID and package name and SHA1 fingerprint.
    g+ create client ID
  7. Now Turn Deep Linking from Google+, later in this tutorial you will learn why you need this.
    g+ deep linking
  8. Note down the Client ID we will use this in our android application project.
    g+ OAuth client ID

 

Integration

In order to use the Google Plus API in our project, we need to integrate the SDK to our project. Use the below code in your build.gradle file and compile.

 

Sign In Users

Before we use Google Plus features, we must sign in with the Google accounts. Google API provides a SignInButton widget for you.

Initialize the GoogleApiClient object, and add the required Scopes.

Note: When you create the sign-in button, keep in mind the branding guidelines. In particular, when you request social scopes, you should use the red sign-in button with the Google+ logo.

 

Getting Profile Information

Use the Plus.PeopleApi.getCurrentPerson method to request profile information the currently signed in user.

You can call the getCurrentPerson method after the GoogleApiClient is connected:

 

Recommend content with the +1 button

Add the native +1 button (PlusOneButton) to your app and configure the button to recommend rich content from your app. Your content must have a corresponding URL.

Refresh the PlusOneButton‘s state each time the activity receives focus in your Activity.onResume handler.

Note: Use of the +1 Button is subject to the Google+ Platform Buttons Policy

 

Sharing to Google+

 

Adding basic sharing

You set up sharing by using the PlusShare.Builder methods.

 g+ sharing post

Adding deep linking to Google+ posts

Deep linking allows the Google+ app on mobile devices to direct clicks on a shared post that contains deep-link information to a resource within your mobile app if the user has it installed. If the user does not have your app installed, they are prompted to install it before accessing the resource. Deep linking can be used on shared basic posts and shared interactive posts.

Sharing interactive posts

Interactive posts provide an easy and prominent way to allow users to share your site or app with their friends and invite them to take a specific action.

g+ interactive post

To know more read from Google Developers.

 

Sign Out User

To Sign-Out the user, clear the default account so that GoogleApiClient will not automatically connect in the future.

 

Revoking Access Tokens and Disconnecting the App

It is highly recommended that you provide users that signed in with Google the ability to disconnect their Google account from your app. If the user deletes their account, you must delete the information that your app obtained from the Google APIs.

Screenshot

google_plus_login

Complete Source code available here.

 


About Durga Chiranjeevi

I’m a normal guy, engineer by education who is passionate about Programming and Internet. An android enthusiast, now moving for cross platforms and game development.

  • Android Developer

    how to show progress indicator in g+ login??

    • Onedevapp

      Hello dev, please use the below code to show progress dialog on login.

      //call this method at onClick of login button
      private void showProgressDialog(String title, String message) {
      if (mProgressDialog != null && mProgressDialog.isShowing())
      mProgressDialog.setMessage(message);
      else
      mProgressDialog = ProgressDialog.show(getActivity(), title, message, true, false);
      }

      //call this method at updateUI for both true and false case too.
      private void hideProgressDialog() {
      if (mProgressDialog != null && mProgressDialog.isShowing()) {
      mProgressDialog.dismiss();
      }
      }

      please do remember that, you may face issues while showing progress dialog on login, since if user has multiple accounts, then an overlay dialog will show up as shown in the image.