Here we will show how to create a Google+ login Account for your Android application Using Android Studio:-
First you have to launch your Android Studio
and then enter into the Configure
Here go for SDK Manager to check whether all the tools required for Google+ app is installed or not
Check you have any update for the Android SDK Tools, and Android SDK Platform tools if any there means just select them and Scroll down in search of Extras.

Google+ is a part of Google play services API hence you have Download the Google Play services in SDK manager

Now Create a new Project
-> Give the Application name for your project that you want and click on Next
->Choose the minimum SDK that you want for your application and click Next
->Choose the blank activity and hit on Next
->Choose the Activity Name that is suitable for your application but better to choose the default activity
Download the Google Play services using Android SDK manger and add dependency to your build.gradle file
Click on the create Client ID button and now you are done with creating your project in Google developer console.
Download the Google Play services using Android SDK manger and add dependency to your build.gradle file
1
2
3
4
5
|
dependencies {
.
.
compile 'com.google.android.gms:play-services:7.5.0'
}
|
Getting the SHA-1 fingerprint which we have to give in Google Developers Console.
Java keytool is used to generate SHA-1 fingerprint. Open your command prompt and execute the following command to generate SHA-1 fingerprint and for passoword enter android if prompted.
For Linux or Mac OS:
For Windows:
Now you will find the generated SHA-1 fingerprint in your terminal

Creating your project in Google Developers Console.
To authenticate and communicate with the Google+ APIs, you must create a Google Developers Console project where you have to enable the Google+ API and create an OAuth 2.0 Client ID.

3.2. Once you are done with creating a project, Click on API’s under API’s and Auth section. Search for the Google+ API and select the one which i have shown in the below image.

3.3. Enable the Google+ API by selecting the Enable API Button available.

3.4.After enabling goto the Credentials section under the API and Create a new Client ID.

3.5. Select the Installed Application as the type and configure consent screen

3.6. Now Fill out the package name of your project, paste the SHA1 finger print, enable the Deep Linking option to activate interactive posts and all the other parameters as shown in the image below .
Click on the create Client ID button and now you are done with creating your project in Google developer console.Step 4: Declare permissions to your mainfest file.
Add a meta-data tag as a child of the application tag to declare the version of Google Play services you are using
To access Google+ APIs:
To retrieve the account name (email) as part of sign-in:
To retrieve OAuth 2.0 tokens or invalidate tokens to disconnect a user. This disconnect option is required to comply with the Google+ Sign-In developer policies:
Finally, This is how your manifest file will after adding meta-data and all the permissions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidsources.googlepluslogin" >
<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:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.androidsources.googlepluslogin.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
</application>
</manifest>
|
eate a class MainActivity.java where we are going to do all our stuff for Google+ Login
- Your class must implement GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener and override all the methods.
- GoogleApiClient is used to communicate with the Google+ API and becomes functional after the asynchronous connection has been established with the service.
- In your OnCreate() method build a GoogleApiClient that uses the desired api.
- While building the GoogleApiClient in onCreate(), If you need access to the user’s identity, social graph and the ability to write app activities, set the scope to PLUS_LOGIN (or) If you just need access to basic profile information, set the scope to profile.
- In your onStart() method start connecting the GoogleApiClient.
- On the Sucessfull connection OnConnected() method is called where we will be using thePlus.PeopleApi.getCurrentPerson method to request profile information the currently signed in user.
- When the GoogleApiClient object is unable to establish a connection onConnectionFailed() is called, where you are passed a ConnectionResult that can be used to resolve the error.
- Google Play services will trigger the onConnectionSuspended callback if our Activity loses its service connection. Here, We will be trying to reconnect the GoogleApiClient.
- Dont forget to disconnect the GoogleApiClient connection in onStop() method else lot of resources will be wasted.
MainActivity.java
Step 6: Create activity_main.xml which will contain our login and logout layout .
This will be the code for signIn button:
activity_main.xml
Thats it folks. You are done with Google+ Login. Follow me on GITHUB

Comments
Post a Comment