Thursday, July 23, 2015

Android Login with Google Plus Account

Android Login with Google Plus Account
Google+ 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. By integrating google plus login in your apps, you can get all the user details in one shot. Not only login, you can do other things like posting to their g+ account, getting list of circles, friends list and lot more. The major advantage of integrating G+ login is, you can drive more users to your app by providing quicker & easiest way of registration process.



Java keytool can be used to generate SHA-1 fingerprint. Open your terminal and execute the following command to generate SHA-1 fingerprint. If it ask for password, type android and press enter.

keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android


C:\Documents and Settings\Administrator>keytool -list -v -keystore "C:\Documents
and Settings\Administrator\.android\debug.keystore" -alias androiddebugkey -sto
repass android -keypass android
Alias name: androiddebugkey
Creation date: Aug 12, 2014
Entry type: PrivateKeyEntry
Certificate chain length: 1
Certificate[1]:
Owner: CN=Android Debug, O=Android, C=US
Issuer: CN=Android Debug, O=Android, C=US
Serial number: 4675c16e
Valid from: Tue Aug 12 15:13:38 GMT+05:30 2014 until: Thu Aug 04 15:13:38 GMT+05
:30 2044
Certificate fingerprints:
        MD5:  48:DF:20:72:26:57:23:04:B5:37:56:83:DF:47:C6:D7
        SHA1: B3:69:85:45:43:4F:B9:94:5B:56:C1:B2:7C:E2:B3:52:85:1E:FE:A2
        Signature algorithm name: SHA256withRSA
        Version: 3

Extensions:

#1: ObjectId: 2.5.29.14 Criticality=false
SubjectKeyIdentifier [
KeyIdentifier [
0000: 93 21 86 69 5B 6A 4D B8   54 2D 40 3F 30 26 CF 9D  .!.i[jM.T-@?0&..
0010: 1A 77 EF 67                                        .w.g
]
]

In the output you can notice SHA1 fingerprint.
SHA1: B3:69:85:45:43:4F:B9:94:5B:56:C1:B2:7C:E2:B3:52:85:1E:FE:A2

1. Installing / updating Google Play Services

Google plus is a part of Google Play Services API. So first we need to download the google play services in Android SDK manager. If you have already installed play services, it it very important to update it to latest version. Open the SDK manager and install or update the play services under Extras section.


On the left, under APIs & auth section, click on APIs and on the right enable Google+ API service.

Now again on the left, click on Credentials and on the right, click on CREATE NEW CLIENT ID button. It will open a popup to configure a new client id.

OAuth

OAuth 2.0 allows users to share specific data with you (for example, contact lists) while keeping their usernames, passwords, and other information private.

Client ID for Android application

Client ID
975044709714-8lk72qsv7p4aqdsdjobmlr67di4av3nj.apps.googleusercontent.com
Redirect URIs
  • urn:ietf:wg:oauth:2.0:oob
  • http://localhost
Package name
com.example.google_login
Certificate fingerprint (SHA1)
B3:69:85:45:43:4F:B9:94:5B:56:C1:B2:7C:E2:B3:52:85:1E:FE:A2
Deep linking
Enabled

Public API access

Use of this key does not require any user action or consent, does not grant access to any account information, and is not used for authorization.

Key for Android applications

API key
AIzaSyAXJRYjMu3VwUoOHeRDwApnPcHRHwvAUuM
Android applications
  • B3:69:85:45:43:4F:B9:94:5B:56:C1:B2:7C:E2:B3:52:85:1E:FE:A2;com.example
Activation date
Jul 22, 2015, 5:12:00 AM
Activated by
ketan.sarvakar@ganpatuniversity.ac.in (you)

Importing Play Services Library to Eclipse

Before creating a new project, we need to import the play services library into Eclipse workspace. To import, follow below steps.
1) In Eclipse goto File Import Android Existing Android Code Into Workspace
2) Click on Browse and select Google Play Services project from your android sdk folder. You can locate play services library project from
android-sdk-windows\extras\google\google_play_services\libproject\google-play-services_lib

E:\kjs\mad\adt-bundle-windows-x86-20140702\adt-bundle-windows-x86-20140702\sdk\extras\google\google_play_services\libproject\google-play-services_lib
3) And check Copy projects into workspace option as shown in the below image, which places a copy of play services in eclipse workspace.


Open the AndroidManifest.xml file add required permissions. Add INTERNET, GET_ACCOUNTS and USE_CREDENTIALS. Also add the meta-data tag for google play services under application tag.

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


After adding, your manifest file should look like this.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
    package="info.androidhive.gpluslogin"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="18" />

    <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="@drawable/ic_launcher"
        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="info.androidhive.gpluslogin.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>
    </application>

</manifest>


Open strings.xml file under res values strings.xml add following strings for buttons text.
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">G+ Login</string>
    <string name="action_settings">Settings</string>
     
    <!-- Button text -->
    <string name="btn_logout_from_google">Logout from Google</string>
    <string name="btn_revoke_access">Revoke Access</string>
</resources>


Now I am designing a simple layout to display user’s profile picture, name, email and other buttons. Paste the below code in layout file of your main activity. In my case my layout file name is activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/llProfile"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:orientation="horizontal"
        android:weightSum="3"
        android:visibility="gone">

        <ImageView
            android:id="@+id/imgProfilePic"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:orientation="vertical"
            android:layout_weight="2" >

            <TextView
                android:id="@+id/txtName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="5dp"
                android:textSize="20dp" />

            <TextView
                android:id="@+id/txtEmail"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="5dp"
                android:textSize="18dp" />
        </LinearLayout>
    </LinearLayout>

    <com.google.android.gms.common.SignInButton
        android:id="@+id/btn_sign_in"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"/>

    <Button
        android:id="@+id/btn_sign_out"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn_logout_from_google"
        android:visibility="gone"
        android:layout_marginBottom="10dp"/>

    <Button
        android:id="@+id/btn_revoke_access"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn_revoke_access"
        android:visibility="gone" />

</LinearLayout>



Continue.....