Learning objectives
- Call the Google+ API from Go.
Now that you've created your Go application on App Engine, let's extend it to search the public Google+ stream for gopher photos.
In order to make a Google+ API call, you first need to import the Google API Client Library for Go.
The Google API Client Library is located at
code.google.com/p/google-api-go-client
.
By convention Go packages are named after their repository URLs, and are installed by the
go
tool in the
$GOPATH
directory.
-
Download the
google-api-go-client
package for Google+ API:/path/to/google_appengine/go get -v -d code.google.com/p/google-api-go-client/plus/v1
-
Go to the
Google Developers Console
and create a new project named
goplus
. - In the APIs and Auth pane, activate Google+ API .
- In the Registered Apps pane, select the name of your app. If your app isn't registered, click Register App .
-
If you haven't done so already, create your application's API key by clicking Create new Key under Public API access . Next, look for your API key in the Key for browser applications or Key for server applications sections.
-
In the
goplus
directory, create a file namedconfig.go
with the following content, and replaceYOUR_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"
-
In the
goplus
directory, create a file namedgopher.go
with the following content:package goplus import ( "net/http" // Import appengine urlfetch package, that is needed to make http call to the api "appengine" "appengine/urlfetch" // Import google api go client library "code.google.com/p/google-api-go-client/googleapi/transport" // Import Google+ package, the package will be named "plus" "code.google.com/p/google-api-go-client/plus/v1" ) // 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) return } // 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) return } // 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) return case "album": http.Redirect(w, r, att.Thumbnails[0].Image.Url, 302) return } } } // 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:
- Set up a plus service using the developer key,
- Call the Search method with the keyword "gopher",
- Scan the result, looking for a photo or album attachment, and
- 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 . - 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