Simplified Buzz sample app
Added README for Buzz sample app
This commit is contained in:
		
							parent
							
								
									5c4323b11c
								
							
						
					
					
						commit
						10d63d385e
					
				| 
						 | 
					@ -0,0 +1,58 @@
 | 
				
			||||||
 | 
					# Buzz Ruby Sample #
 | 
				
			||||||
 | 
					This is a simple starter project written in Ruby which provides a minimal
 | 
				
			||||||
 | 
					example of Buzz integration within a Sinatra web application.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Once you've run the starter project and played with the features it provides,
 | 
				
			||||||
 | 
					this starter project provides a great place to start your experimentation into
 | 
				
			||||||
 | 
					the API.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## Prerequisites ##
 | 
				
			||||||
 | 
					Please make sure that all of these are installed before you try to run the
 | 
				
			||||||
 | 
					sample.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					- Ruby 1.8.7+
 | 
				
			||||||
 | 
					- Ruby Gems 1.3.7+
 | 
				
			||||||
 | 
					- Are you on a Mac? If so, be sure you have XCode 3.2+
 | 
				
			||||||
 | 
					- A few gems (run 'sudo gem install <gem name>' to install)
 | 
				
			||||||
 | 
					-- sinatra
 | 
				
			||||||
 | 
					-- httpadapter
 | 
				
			||||||
 | 
					-- extlib
 | 
				
			||||||
 | 
					-- dm-sqlite-adapter
 | 
				
			||||||
 | 
					-- google-api-ruby-client
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## Setup Authentication ##
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					This API uses OAuth 2.0. Learn more about Google APIs and OAuth 2.0 here:
 | 
				
			||||||
 | 
					http://code.google.com/apis/accounts/docs/OAuth2.html
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Or, if you'd like to dive right in, follow these steps.
 | 
				
			||||||
 | 
					 - Visit https://code.google.com/apis/console/ to register your application.
 | 
				
			||||||
 | 
					 - From the "Project Home" screen, activate access to "Buzz API".
 | 
				
			||||||
 | 
					 - Click on "API Access" in the left column
 | 
				
			||||||
 | 
					 - Click the button labeled "Create an OAuth2 client ID"
 | 
				
			||||||
 | 
					 - Give your application a name and click "Next"
 | 
				
			||||||
 | 
					 - Select "Web Application" as the "Application type"
 | 
				
			||||||
 | 
					 - Under "Your Site or Hostname" select http:// as the protocol and enter
 | 
				
			||||||
 | 
					   "localhost" for the domain name
 | 
				
			||||||
 | 
					 - click "Create client ID"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Edit the buzz.rb file and enter the values for the following properties that 
 | 
				
			||||||
 | 
					you retrieved from the API Console:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 - oauth_client_id
 | 
				
			||||||
 | 
					 - oauth_client_secret
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Or, include them in the command line as the first two arguments.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## Running the Sample ##
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					I'm assuming you've checked out the code and are reading this from a local
 | 
				
			||||||
 | 
					directory. If not check out the code to a local directory.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					1. Start up the embedded Sinatra web server
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  $ ruby buzz.rb
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					2. Open your web browser and see your activities! Go to http://localhost:4567/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					3. Be inspired and start hacking an amazing new web app!
 | 
				
			||||||
							
								
								
									
										125
									
								
								examples/buzz.rb
								
								
								
								
							
							
						
						
									
										125
									
								
								examples/buzz.rb
								
								
								
								
							| 
						 | 
					@ -1,84 +1,125 @@
 | 
				
			||||||
$:.unshift('lib')
 | 
					$:.unshift('lib')
 | 
				
			||||||
 | 
					#####!/usr/bin/ruby1.8
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Copyright:: Copyright 2011 Google Inc.
 | 
				
			||||||
 | 
					# License:: All Rights Reserved.
 | 
				
			||||||
 | 
					# Original Author:: Bob Aman
 | 
				
			||||||
 | 
					# Maintainer:: Daniel Dobson (mailto:wolff@google.com)
 | 
				
			||||||
 | 
					# Maintainer:: Jenny Murphy (mailto:mimming@google.com)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
require 'rubygems'
 | 
					require 'rubygems'
 | 
				
			||||||
require 'sinatra'
 | 
					require 'sinatra'
 | 
				
			||||||
require 'datamapper'
 | 
					 | 
				
			||||||
require 'google/api_client'
 | 
					require 'google/api_client'
 | 
				
			||||||
 | 
					require 'httpadapter/adapters/net_http'
 | 
				
			||||||
 | 
					require 'pp'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use Rack::Session::Pool, :expire_after => 86400 # 1 day
 | 
					use Rack::Session::Pool, :expire_after => 86400 # 1 day
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Set up our token store
 | 
					# Configuration
 | 
				
			||||||
DataMapper.setup(:default, 'sqlite::memory:')
 | 
					# See README for getting API id and secret
 | 
				
			||||||
class TokenPair
 | 
					 | 
				
			||||||
  include DataMapper::Resource
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  property :id, Serial
 | 
					if (ARGV.size < 2)
 | 
				
			||||||
  property :refresh_token, String
 | 
					  set :oauth_client_id, 'oauth_client_id'
 | 
				
			||||||
  property :access_token, String
 | 
					  set :oauth_client_secret, 'oauth_client_secret'
 | 
				
			||||||
  property :expires_in, Integer
 | 
					
 | 
				
			||||||
  property :issued_at, Integer
 | 
					  if (settings.oauth_client_id == 'oauth_client_id')
 | 
				
			||||||
 | 
					    puts 'See README for getting API id and secret.  Server terminated.'
 | 
				
			||||||
 | 
					    exit(0)
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					else
 | 
				
			||||||
 | 
					  set :oauth_client_id, ARGV[0]
 | 
				
			||||||
 | 
					  set :oauth_client_secret, ARGV[1]
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Configuration that you probably don't have to change
 | 
				
			||||||
 | 
					set :oauth_scopes, 'https://www.googleapis.com/auth/buzz'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class TokenPair
 | 
				
			||||||
 | 
					  @refresh_token
 | 
				
			||||||
 | 
					  @access_token
 | 
				
			||||||
 | 
					  @expires_in
 | 
				
			||||||
 | 
					  @issued_at
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def update_token!(object)
 | 
					  def update_token!(object)
 | 
				
			||||||
    self.refresh_token = object.refresh_token
 | 
					    @refresh_token = object.refresh_token
 | 
				
			||||||
    self.access_token = object.access_token
 | 
					    @access_token = object.access_token
 | 
				
			||||||
    self.expires_in = object.expires_in
 | 
					    @expires_in = object.expires_in
 | 
				
			||||||
    self.issued_at = object.issued_at
 | 
					    @issued_at = object.issued_at
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def to_hash
 | 
					  def to_hash
 | 
				
			||||||
    return {
 | 
					    return {
 | 
				
			||||||
      :refresh_token => refresh_token,
 | 
					      :refresh_token => @refresh_token,
 | 
				
			||||||
      :access_token => access_token,
 | 
					      :access_token => @access_token,
 | 
				
			||||||
      :expires_in => expires_in,
 | 
					      :expires_in => @expires_in,
 | 
				
			||||||
      :issued_at => Time.at(issued_at)
 | 
					      :issued_at => Time.at(@issued_at)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
TokenPair.auto_migrate!
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# At the beginning of any request, make sure the OAuth token is available.
 | 
				
			||||||
 | 
					# If it's not available, kick off the OAuth 2 flow to authorize.
 | 
				
			||||||
before do
 | 
					before do
 | 
				
			||||||
  @client = Google::APIClient.new
 | 
					  @client = Google::APIClient.new(
 | 
				
			||||||
  @client.authorization.client_id = '245083617981.apps.googleusercontent.com'
 | 
					    :authorization => :oauth_2,
 | 
				
			||||||
  @client.authorization.client_secret = 'pYelZCRjSa+iMezYENVScXFk'
 | 
					    :host => 'www.googleapis.com',
 | 
				
			||||||
  @client.authorization.scope = 'https://www.googleapis.com/auth/buzz'
 | 
					    :http_adapter => HTTPAdapter::NetHTTPAdapter.new
 | 
				
			||||||
 | 
					  )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @client.authorization.client_id = settings.oauth_client_id
 | 
				
			||||||
 | 
					  @client.authorization.client_secret = settings.oauth_client_secret
 | 
				
			||||||
 | 
					  @client.authorization.scope = settings.oauth_scopes
 | 
				
			||||||
  @client.authorization.redirect_uri = to('/oauth2callback')
 | 
					  @client.authorization.redirect_uri = to('/oauth2callback')
 | 
				
			||||||
  @client.authorization.code = params[:code] if params[:code]
 | 
					  @client.authorization.code = params[:code] if params[:code]
 | 
				
			||||||
  if session[:token_id]
 | 
					  if session[:token]
 | 
				
			||||||
    # Load the access token here if it's available
 | 
					    # Load the access token here if it's available
 | 
				
			||||||
    token_pair = TokenPair.get(session[:token_id])
 | 
					    @client.authorization.update_token!(session[:token].to_hash)
 | 
				
			||||||
    @client.authorization.update_token!(token_pair.to_hash)
 | 
					 | 
				
			||||||
  end
 | 
					 | 
				
			||||||
  if @client.authorization.refresh_token && @client.authorization.expired?
 | 
					 | 
				
			||||||
    @client.authorization.fetch_access_token!
 | 
					 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @buzz = @client.discovered_api('buzz')
 | 
					  @buzz = @client.discovered_api('buzz')
 | 
				
			||||||
  unless @client.authorization.access_token || request.path_info =~ /^\/oauth2/
 | 
					  unless @client.authorization.access_token || request.path_info =~ /^\/oauth2/
 | 
				
			||||||
    redirect to('/oauth2authorize')
 | 
					    redirect to('/oauth2authorize')
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Part of the OAuth flow
 | 
				
			||||||
get '/oauth2authorize' do
 | 
					get '/oauth2authorize' do
 | 
				
			||||||
  redirect @client.authorization.authorization_uri.to_s, 303
 | 
					  <<OUT
 | 
				
			||||||
 | 
					<!DOCTYPE html>
 | 
				
			||||||
 | 
					<html>
 | 
				
			||||||
 | 
					<head>
 | 
				
			||||||
 | 
					  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 | 
				
			||||||
 | 
					  <title>Google Ruby API Buzz Sample</title>
 | 
				
			||||||
 | 
					</head>
 | 
				
			||||||
 | 
					<body>
 | 
				
			||||||
 | 
					<header><h1>Google Ruby API Buzz Sample</h1></header>
 | 
				
			||||||
 | 
					<div class="box">
 | 
				
			||||||
 | 
					<a class='login' href='#{@client.authorization.authorization_uri.to_s}'>Connect Me!</a>
 | 
				
			||||||
 | 
					</div>
 | 
				
			||||||
 | 
					</body>
 | 
				
			||||||
 | 
					</html>
 | 
				
			||||||
 | 
					OUT
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Part of the OAuth flow
 | 
				
			||||||
get '/oauth2callback' do
 | 
					get '/oauth2callback' do
 | 
				
			||||||
  @client.authorization.fetch_access_token!
 | 
					  @client.authorization.fetch_access_token!
 | 
				
			||||||
  # Persist the token here
 | 
					  unless session[:token]
 | 
				
			||||||
  token_pair = if session[:token_id]
 | 
					    token_pair = TokenPair.new
 | 
				
			||||||
    TokenPair.get(session[:token_id])
 | 
					 | 
				
			||||||
  else
 | 
					 | 
				
			||||||
    TokenPair.new
 | 
					 | 
				
			||||||
  end
 | 
					 | 
				
			||||||
    token_pair.update_token!(@client.authorization)
 | 
					    token_pair.update_token!(@client.authorization)
 | 
				
			||||||
  token_pair.save()
 | 
					    # Persist the token here
 | 
				
			||||||
  session[:token_id] = token_pair.id
 | 
					    session[:token] = token_pair
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
  redirect to('/')
 | 
					  redirect to('/')
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# The method you're probably actually interested in. This one lists a page of your
 | 
				
			||||||
 | 
					# most recent activities
 | 
				
			||||||
get '/' do
 | 
					get '/' do
 | 
				
			||||||
  result = @client.execute(
 | 
					  response = @client.execute(
 | 
				
			||||||
    @buzz.activities.list,
 | 
					    @buzz.activities.list,
 | 
				
			||||||
    {'userId' => '@me', 'scope' => '@consumption', 'alt'=> 'json'}
 | 
					    'userId' => '@me', 'scope' => '@consumption', 'alt'=> 'json'
 | 
				
			||||||
  )
 | 
					  )
 | 
				
			||||||
  status, _, _ = result.response
 | 
					  status, headers, body = response
 | 
				
			||||||
  [status, {'Content-Type' => 'application/json'}, JSON.generate(result.data)]
 | 
					  [status, {'Content-Type' => 'application/json'}, body]
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue