Today I would like to tell you about several interesting things. It is Google JavaScript API v3 and new generation of jQuery Templates – JsRender. I will show you how to setup authentication with Google (OAuth 2.0) and perform some API requests. One of them is obtaining info of logged-in member, and second one is obtaining an activity feed of your friends. Another side of our tutorial is jQuery templating. I will show you how to render HTML content using templates.
I hope that you’ve already created your own API project in the Google APIs Console as I’ve described recently in my Google API – Get contact list tutorial. If you haven’t – please do it and create your own Google API project. Next step is enabling of ‘Google+ API’, so, please open your Google API console and then select ‘Services’. Here we should Enable ‘Google+ API’:

After, please open ‘API Access’ and copy your ‘Client ID’ and ‘API key’ (at the end) keys:

Now we are ready to start coding !
[sociallocker]
[/sociallocker]
Step 1. HTML
Right now create an empty index.html file and put next code:
index.html
004 | <meta charset="utf-8" /> |
005 | <title>How to get your Google+ profile info and friend's activity using the Google JS API | Script Tutorials</title> |
006 | <link href="css/main.css" rel="stylesheet" type="text/css" /> |
009 | google.load("jquery", "1.7.1"); |
011 | <script type="text/javascript" src="js/jsrender.js"></script> |
013 | <script id="gplusTemplate" type="text/x-jsrender"> |
014 | <div id="{{>id}}" class="gp"> |
015 | <img src="{{:actor.image.url}}" /> |
017 | <p><a href="{{:actor.url}}" target="_blank">{{:actor.displayName}}</a><span>{{:published}}</span></p> |
018 | <p><a href="{{:url}}" target="_blank">{{:title}}</a></p> |
019 | <p>{{:object.content}}</p> |
026 | <h2>How to get your Google+ profile info and friend's activity using the Google JS API</h2> |
029 | <img src="google-plus.jpg" class="google" alt="google plus" /> |
031 | <button id="login" style="visibility: hidden">Authorize</button> |
032 | <div id="author"></div> |
033 | <div id="actList"></div> |
035 | <script type="text/javascript"> |
037 | var clientId = 'YOUR_CLIENT_ID_KEY'; |
039 | var apiKey = 'YOUR_API_KEY'; |
040 | // The authorization scope |
042 | // onliad initialization |
043 | function onloadInitialization() { |
044 | // set our own api key |
045 | gapi.client.setApiKey(apiKey); |
046 | // and checkAuth in 1 ms |
047 | window.setTimeout(checkAuth, 1); |
049 | // check authorization |
050 | function checkAuth() { |
051 | gapi.auth.authorize({ |
055 | }, handleAuthResult); |
057 | // handle authorization result |
058 | function handleAuthResult(authResult) { |
059 | var authorizeButton = document.getElementById('login'); |
060 | if (authResult && ! authResult.error) { |
061 | authorizeButton.style.visibility = 'hidden'; |
062 | makeGoogleApiCalls(); |
064 | authorizeButton.style.visibility = ''; |
065 | authorizeButton.onclick = handleAuthClick; |
068 | // handle onclick event of Login button |
069 | function handleAuthClick(event) { |
070 | gapi.auth.authorize({ |
074 | }, handleAuthResult); |
077 | // make Google API calls: obtain logged-in member info and activity of friends |
078 | function makeGoogleApiCalls() { |
079 | gapi.client.load('plus', 'v1', function() { |
080 | // Request1: obtain logged-in member info |
081 | var request = gapi.client.plus.people.get({ |
084 | request.execute(function(aInfo) { |
085 | // prepare author info array for rendering |
089 | 'actor': {'image': {'url': aInfo.image.url}, 'url': aInfo.url, 'displayName': aInfo.displayName}, |
092 | 'title': 'My page at G+', |
093 | 'object': {'content': ''} |
096 | // and render it using 'gplusTemplate' template |
098 | $('#gplusTemplate').render(authorInfo) |
101 | // Request2: obtain friend's activity feed |
102 | var restRequest = gapi.client.request({ |
103 | 'path': '/plus/v1/activities', |
104 | 'params': {'query': 'Google+', 'orderBy': 'best'} |
106 | restRequest.execute(function(activityInfo) { |
107 | // render activity items using 'gplusTemplate' template |
109 | $('#gplusTemplate').render(activityInfo.items) |
In the header of our file we load jQuery library, jsrender.js (JsRender library) and define custom HTML template (gplusTemplate). We will use this template to generate G+ friend’s activity list.
In the body of our page we have ‘Authorize’ button and 2 containers (to display our own info and activity list:
1 | <button id="login" style="visibility: hidden">Authorize</button> |
2 | <div id="author"></div> |
3 | <div id="actList"></div> |
Now we should understand our JS code. Important note – please put your own ClientID and API keys in the beginning of this JavaScript code. Several functions in the middle are for handling with Login button (authorization). When the page has loaded we set API key and set timeout to check authorization. If we are logged in – we hide Login button. Otherwise – display it and add ‘onclick’ handler (to evoke authorization). The standard authorize() method always shows a popup window, which can be a little annoying if you are just trying to refresh an OAuth 2.0 token. Google’s OAuth 2.0 implementation also supports “immediate” mode, which refreshes a token without a popup window.
Well, when we are logged in – we can make Google API calls. We are going to make 2 api requests: get member info and get friends activity. This is very easy:
Well, when we are logged in – we can make Google API calls. We are going to make 2 api requests: get member info and get friends activity. This is very easy:
02 | var request = gapi.client.plus.people.get({ |
05 | request.execute(function(aInfo) { |
09 | var restRequest = gapi.client.request({ |
10 | 'path': '/plus/v1/activities', |
11 | 'params': {'query': 'Google+', 'orderBy': 'best'} |
13 | restRequest.execute(function(activityInfo) { |
Next part is very interesting too. As you can see – as usual we get response in JSON format. Thus I have a question – how to use data from JSON response in order to generate result HTML code? Some people can offer ‘for’ method (to pass through all the elements of the JSON array), but I would like you to suggest a new method – templates. It has one important advantage – speed. The result speed can be very high especially during generation of big lists. Look at this code and you will understand that this is very easy to render html content using JsRender library:
01 | var restRequest = gapi.client.request({ |
02 | 'path': '/plus/v1/activities', |
03 | 'params': {'query': 'Google+', 'orderBy': 'best'} |
05 | restRequest.execute(function(activityInfo) { |
08 | $('#gplusTemplate').render(activityInfo.items) |
You can download JsRender jQuery library here. JsRender templates Syntax:
| Description | Example | Output |
| Value of firstName property of the data item with no encoding | {{:firstName}} | Madelyn |
| Simple object path to nested property, with no encoding | {{:movie.releaseYear}} | 1987 |
| Simple comparison | {{:movie.releaseYear < 2000}} | true |
| Value with no encoding | {{:movie.name}} | Star Wars IV: Return of the Jedi |
| HTML-encoded value | {{>movie.name}} | Star Wars: Episode VI: <span style=’color:purple;font-style: italic;’>Return of the Jedi</span> |
| HTML-encoded value | {{html:movie.name}} | Star Wars: Episode VI: <span style=’color:purple;font-style: italic;’>Return of the Jedi</span> |
Conclusion
If you have any suggestions about further ideas for articles – you are welcome to share them with us. Good luck in your work!