Hello Google+

Now that you've created your Go application on App Engine, you will extend it to search the public Google+ stream for gopher photos.


  • Set up the Google API Client Library for Go
  • Create Google Cloud Platform Console project and enable the Google+ API
  • Call the Google+ API from Go.

In order to make a Google+ API call, you first need to import the Google API Client Library for Go.

The Google API Client Libraries can be found under google.golang.org/api. By convention Go packages are named after their repository URLs, and are installed by the go tool in the $GOPATH directory.

  1. Download the google-api-go-client package for Google+ API:

    /path/to/google_appengine/go get -v -d google.golang.org/api/plus/v1
  2. Enable the Google+ API. Choose the App Engine project that you created in the previous lesson. Click Continue.

  3. If you haven't done so already, create your application's API key by clicking Create credentials > API key. Next, look for your API key in the API keys section.

  4. In the goplus directory, create a file named config.go with the following content, and replace YOUR_API_KEY with the api key you just generated.

    package goplus
    // apiKey is the Google API key of the project copied from the Google API Console.
    var apiKey = "YOUR_API_KEY"
  5. In the goplus directory, edit the app.yaml file and replace application: goplus with the project ID you chose in the previous lesson.

    application: YOUR_PROJECT_ID
  6. In the goplus directory, create a file named gopher.go with the following content:

    package goplus
    import (
    	// Import appengine urlfetch package, that is needed to make http call to the api
    	// Import google api go client library
    	// Import Google+ package, the package will be named "plus"
    // gopherFallback is the official gopher URL (in case we don't find any in the Google+ stream)
    const gopherFallback = "http://golang.org/doc/gopher/gophercolor.png"
    // init is called before the application start
    func init() {
    	// Register a handler for /gopher URLs.
    	http.HandleFunc("/gopher", gopher)
    // gopher is an HTTP handler that searches Google+ for an activity
    // with a Gopher photo and redirects to the image thumbnail.
    func gopher(w http.ResponseWriter, r *http.Request) {
    	// Create appengine context, needed to do urlfetch call
    	c := appengine.NewContext(r)
    	// Create a new http client, supplying the API key we
    	// generated to identify our application, and the urlfetch
    	// transport necessary to make HTTP calls on App Engine
    	transport := &transport.APIKey{
    		Key:       apiKey,
    		Transport: &urlfetch.Transport{Context: c}}
    	client := &http.Client{Transport: transport}
    	// Create the plus service object with which we can make an API call
    	service, err := plus.New(client)
    	if err != nil {
    		// error creating the Google+ client
    		http.Error(w, err.Error(), http.StatusInternalServerError)
    	// Search the stream for "gopher" related activities
    	result, err := service.Activities.Search("#gopher").Do()
    	if err != nil {
    		// error searching Google+ for gopher
    		http.Error(w, err.Error(), http.StatusInternalServerError)
    	// Iterate through the activities search result until we find a photo or album attachment
    	for _, item := range result.Items {
    		for _, att := range item.Object.Attachments {
    			switch att.ObjectType {
    			case "photo":
    				// Redirect to the gopher thumbnail
    				http.Redirect(w, r, att.Image.Url, 302)
    			case "album":
    				http.Redirect(w, r, att.Thumbnails[0].Image.Url, 302)
    	// Fallback on the official gopher if we didn't find any gophers in the stream
    	http.Redirect(w, r, gopherFallback, http.StatusFound)

    The gopher function does several things:

    1. Set up a plus service using the developer key,
    2. Call the Search method with the keyword "gopher",
    3. Scan the result, looking for a photo or album attachment, and
    4. Redirect the client to the first image URL found.

    The google-api-go-client provides a set of Go types with methods for interacting with the Google+ API as you would with any native package. These types are generated automatically using the Google APIs Discovery Service.

  7. Visit http://localhost:8080/gopher in your browser.

You should be redirected to the most recent Gopher-related picture posted on Google+.

Next step: Deploy to App Engine

Send feedback about...

App Engine standard environment for Go