Monday, March 31, 2014

Android Programming With the Google+ API Overview

Since its launch in 2011 Google+ has gained a reputation and following that seems to be growing by leaps and bounds. As more traffic jumps on the Google+ bandwagon; more and more developers are looking for ways to incorporate Google+ functionality into their apps.

This article discusses concepts and tools associated with developing Android applications that use the Google+ API to: 
  • Present Google+ log-in and Consent Form to users;
  • Create Google+ posts;
  • Access and use user activity data (such as game scores, purchases, etc.); and
  • Search and retrieve Google+ people information (for example, listing people with the highest game scores, etc).
It also discusses the Google Play Services public interfaces associated with the Android Quick Start app.

Getting Started


To develop an Android application on the Google+ platform you will need Google Play Services and the latest version of the SDK Tools, which can be added or updated using the Android SDK Manager.

The Android SDK Manager can be accessed from within the Eclipse IDE (with the Android Development Tools) by selecting Window -> Android SDK Manager. You can then select to install or update Google Play Services and the SDK Tools, as shown in the following picture. For more information see my previous article Programming Mobile Apps for Android With Eclipse and Getting Started With the Google+ Platform.


Note that you may even need to update the Android Developer Tools (ADT) plugin if you are using Eclipse to build Android apps. If you need to update the ADT you can do so (from within Eclipse) by selecting  Help -> Install New Software. The Available Software window displays. When you click the Add button the Add Repository window (shown below) displays. You can then add the URL for the ADT: https://dl-ssl.google.com/android/eclipse/


Once you add the URL and click OK the wizard walks you through selecting the components you can install to update an existing installation.


For additional information on updating the ADT in Eclipse visit  http://developer.android.com/sdk/installing/installing-adt.html.

Installing or updating Google Play Services, SDK tools, ADT, are just a few of the updates you may need to make to your development environment. For a complete list of the prerequisites needed to use the Google+ API you can visit: https://developers.google.com/+/mobile/android/getting-started#prerequisites.

Google Developers Console

Once you have prepared your development environment as previously mentioned, you need to enable the Google+ API. To begin you access the Google Developers Console and create a project. The Console automatically assigns a unique project name.


Once the project is created you can select the project to set the properties.


In this example I created a project to run the Google Quick Start sample project. I can now enable the Google+ API by clicking the Off button to turn On the Google+ API.

You will also need a unique Client ID, which is created by clicking the Create New Client ID button shown in the following picture. The Create Client ID window, also shown below, displays. You will then need to add the application type (which is "Installed" for Android apps) and package name (I used: com.google.android.gms.plus.sample.quickstart as the package name so I could run the Quick Start Sample). You will also need the Signing certificate fingerprint (SHA1) (see my previous article How to Build Facebook Apps and Games for Android to learn more about hash keys). Once all fields are completed you can save your changes. 



Once the information has been entered and submitted the client ID is created, as shown in the following picture. In this example the client ID is associated with the package name of the application (shown in the following picture). Therefore, if you add a package name that is different from that of the package name in the application your sample application will not work.



As API requests are sent, from your application, to the Google Server the client ID is used to identify your application and also track associated transactions.


After you enable the Google+ API (application programming interface) you can configure your development environment to make Google+ API calls via Google Play Services.

Once you create your project in the Google Developers Console you must then clone or download the sample application from GitHub:  https://github.com/googleplus/gplus-quickstart-android.

Configuring the Android Quick Start App

As previously mentioned, your project will use Google Play Services to access the Google+ API. Therefore, part of preparing your development environment includes importing the Google Play Services library located in the adt-bundle directory on your computer. (Note: I'm running Windows 64-bit so my adt-bundle directory is as follows:  adt-bundle-windows-x86_64-20131030). The path to the Google Play Services library on my computer is as follows:


Use File -> Import from within the Eclipse IDE to select the Google Play Services Library project.
Select the Google Play Services lib project, as shown below. Click Finish to import the selected project.

You will then need to reference the google-play-services_lib project from within your project.  To run the sample I must first reference google-play-services_lib.To do this I right-clicked the project and select properties from the menu to display the Project Properties window, shown below.


I can then click the Add button and select the google-play-services_lib (which is available because you added it as a project to the workspace).


Once you have added the project you can click OK to close the Project Properties window.



To run the sample you must connect to a real Android device. Since your project is not configured to run on a specific device (real or virtual) when you select Run, from within the Eclipse IDE, the Android Device Chooser displays (as shown below).


I can then click OK to run the device. For more information about running virtual versus real devices see my previous article titled, Programming Mobile Apps for Android With Eclipse.

OAuth 2.0 Protocol


The Google+ authentication/authorization uses the OAuth 2.0 framework. The OAuth 2.0 framework was created so third-party applications could access another company's resources (mainly social networking company resources) without giving these applications customer's private data.

With the OAuth 2.0 Framework a users' log-in, for example, is neither accessed nor stored by a third-party app. And, Google is just one of many companies that currently use the OAuth 2.0 Framework.Google's implementation of the OAuth 2.0 framework supports three types of applications: Web Server apps, computer and hand-held device apps and apps that run on the client-side (such as JavaScript applications).

To implement the Google+ login, which utilizes the OAuth 2.0 framework for authentication and authorization, developers can use Google+ log-in, which invokes the user Consent Form as well as the Google user login window.


API programming really is all about code re-use, which makes developing apps easier and faster. Consider the development time cut by building an application on top of one or more existing applications (in this case Google+ and Google Play Services). You do not have to write as much code to build your application. Instead, you use your knowledge of the existing applications (by reviewing the API documentation) and then declaring variables/objects and calling the methods, etc. you want to use or setting the applicable properties to fit your application. Notice below the code to import the Login button and then the code that declares and calls the Login button. A procedure was not written to create the Login button because it already exists.



When the user click the Google+ Sign in button the user is presented with the Google sign-in. To login the user enters the correct user name and password and submits the response. The Consent form is also presented to the user. The user reviews the form and makes desired changes to the Consent form if the application requests access the user does not want to give. The information is sent to Google Play Services. If the credentials are accurate the Google server returns an authorization code to your application. Your application then requests an access token. The Google server returns an access token to your application.




The access token is used, by your application, to perform the approved transactions on behalf of the user. The response from the server also includes a refresh token. Whenever the access token expires the refresh token is used to request a new access token until the refresh token expires. For more information on OAuth 2.0 see Google's OAuth 2.0 Playground . For more on Google+ sign in for Android see: https://developers.google.com/+/mobile/android/sign-in and https://developers.google.com/+/features/sign-in

Google Play Services & The Google+ API


In the Quick Start app, the googleapiclient is the "main entrypoint for Google Play Services integration" so that calls from the application to Google Play are wrapped with the googleapiclient interface.  Once a connection is established the People class is used to get information associated the current person logged in. The Quick Start app calls the AccountAPI to clear the default account or revoke access when the user selects the applicable option. The following paragraphs provide additional information about the Google+ API.

The Google+ API is divided into four resource types as follows:  People, Activities, Comments and Moments

People Resource: The People resource contains the method used to get people data by retrieving the individual's user name  (or userID). A few of the properties associated with the People resource include the person's name, profile image, profile URL, about me content (including the about me description, bragging rights, relationship status), organization where the person works/worked, places lived, circled-by count, plus-one count, whether or not the account is verified, the cover photo and more. Developers can incorporate some or all of the attributes within an application that uses the Google+ API. The developer can also use the People resource to search for people, list people by activity (discussed below), etc.


Activities Resource: The Activities resource is used to work with content posted to Google+. The Activities resource has three property types as follows: Actor, Object and Verb. The Actor property type includes properties associated with the person who performs an activity such as create a post, re-share a post or even plus-one a post. A few Actor properties include display name, the URL for the person's profile, the URL for the person's profile image, etc.The Verb property type defines whether content was created (i.e., newly published content) or re-shared. The Object property includes properties such as the object type for example whether the activity is associated with content or an action such as plus-one or re-share.It also includes properties that hold the ID of the object, the original actor, comments (or replies) associated with the published content, people who +1'd the content, etc.

Comments Resource: The Comments resource includes methods used to list comments associated with content or get content.

Moments Resource: The Moments resource includes methods used to record activities users perform.For example, if a user makes a purchase or achieves a high score in a game; developers can use the moments.insert method to create a post that discloses the applicable information.

Lastly, developers use App Activities to create actions users can perform within an application such as buying, adding comments, identifying the user's location, etc.