2015 / 31 May

Using Google+ API for Quick Log-In & Prototyping


The other day, a friend was talking to me about an “idea” that he had for a mobile-application. To be honest, it was a pretty stellar idea, but he complained about the time constraints he had. He mentioned how just getting the prototype up-and-running, which would have to include a log-in form and somehow getting the users information, would require the effort and time that simply didn’t excite him.

Well, the good news is that with the access to the current APIs we have, the first two steps can be completely trimmed down to around half-an hour of development (give or take 😛 )

In this post, I’ll show you guys how you can use the Google+ API with ease to begin your prototyping.

1) Consuming the G+ Api via the Google Console

    – First open the Google API Console : https://code.google.com/apis/console

    – You’ll find “APIs & Auth” menu on the left hand side.

googleApi


    – Click on “APIs” and search for “Google+ API” on the panel on the right.

    – Enable the Google+ API



Cool, now remember those pop-ups you get everytime you install a new application and it asks for your “Approval” to share info? Well, now we’re gonna set that up for your app. (This has the added benefit of making your app look more professional 😉 )

ConsentScreen

    – On the same left hand panel as before, under APIs & Auth, click on “Consent Screen”

    – Type in the Product Name (your app’s name) and then click “Save”


Now you wanna set up your credentials using OAuth. This will allow users to share information with you after logging in, but will keep their login-info private. 🙂

CreateClientID

    – Under the “APIs” menu, click on “Credentials” this time.

    – On the panel on the right hand side, click “Create New Client ID”

    – Select “Installed Application” under application type.

    – Select your application type (In our case: Android)

    – Enter the package name used in your app.

    – Enable Deep Linking.


You’re gonna notice that you cant click “Create Client ID” first. That’s because you need to generate and enter your certificate fingerprint. It’s for security, but don’t fret it’s easy:

    – The Java keytool can be used to generate an SHA-1 fingerprint within a few seconds.

SHAExample



    If you’re on a Mac OS or Linux, just navigate to ~/.android/ and run the keytool using the debug.keystore (located at ~/.android/debug.keystore) (Typically installed here, not always)

keytool -exportcert -alias androiddebugkey -keystore -list -v

    – If you’re on Windows, navigate to %USERPROFILE%\.android\ (Typically installed here, not always) and run the keytool using the debug.keystore

keytool -exportcert -alias androiddebugkey -keystore -list -v

    – It’s gonna ask you for a password : Enter “android”.

    – Look for “SHA1” in the generated fingerprints and copy that from your terminal.



See? That wasn’t so hard 😛


    – Now copy paste that SHA into your “Create Client ID” screen.

    – Make sure Deep Linking is enabled and click “Create Client ID”

    You’re ready to go.


2 ) Configuring Eclipse/Android Studio for Google+ API

Alright, so the next step is to actually import the G+ API into your environment. We’re gonna simplify this process and use Maven. If you haven’t checked it out already, check out my blog post on How To Use Maven to Simplify Your Development Life.

    – Add the following build-script to your pom.xml if you’re using Maven in Eclipse:

[xml]
<project>
<dependencies>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-plusDomains</artifactId>
<version>v1-rev121-1.20.0</version>
</dependency>
</dependencies>
</project>
[/xml]

    – If, however, you’re using Android Studio, you want the gradle dependencies. Add this to your build.gradle file:

[xml]
repositories {
mavenCentral()
}

dependencies {
compile ‘com.google.apis:google-api-services-androidpublisher:v1-rev56-1.20.0’
}
[/xml]

 

 

That’s it ! You’re good to go!!


3) Initializing GoogleAPI Client in your application


    – After adding the maven/gradle dependencies, you can use the GoogleApiClient object to initialize your login:

private GoogleApiClient myGoogleApi;

– Then, in your onCreate() , you want to call a helper method to initialize your object. (Just to make things neater)


[java]
initGoogleApi(){

this.myGoogleApi = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.build();
}
[/java]

– Similary, in you handle the onStart() and onStop() methods with the “.connect()”, “isConnected()” and “.disconnect()” calls.

[java]
protected void onStart() {
super.onStart();
myGoogleApi.connect();
}

protected void onStop() {
super.onStop();

if (myGoogleApi.isConnected()) {
myGoogleApi.disconnect();
}
}

[/java]

 

– You’re done! You can design your UI to be as plain and simple or as cool as you want. If you really want to speed things up, I suggest using the the Google+ Sign-in Button element provided by google. It has the added benefit of looking cool 😉

 

<com.google.android.gms.common.SignInButton
        android:id="@+id/signIn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
GoogleSignIn

No comments so far.

LEAVE YOUR COMMENT