Skip to content

Instantly share code, notes, and snippets.

@kgbu
Created June 9, 2010 02:42
Show Gist options
  • Select an option

  • Save kgbu/430969 to your computer and use it in GitHub Desktop.

Select an option

Save kgbu/430969 to your computer and use it in GitHub Desktop.
Still buggy Buzz client on Sinatra
require 'rubygems'
require 'sinatra'
require 'oauthclient'
require 'zlib'
require 'stringio'
configure do
Consumer_key = "nantara.domain"
Consumer_secret = "kantara_secret"
Scope = 'https://www.googleapis.com/auth/buzz'
Request_token_url = 'https://www.google.com/accounts/OAuthGetRequestToken'
Access_token_url = 'https://www.google.com/accounts/OAuthGetAccessToken'
Callback = 'http://www.jin.gr.jp/buzztra/access_token'
# Session
use Rack::Session::Cookie,
#:key => 'rack.session',
:domain => 'mydomain',
:path => '/',
:expire_after => 3600,
:secret => Digest::SHA1.hexdigest(rand.to_s)
end
before do
unless @client
@client = OAuthClient.new
@client.oauth_config.consumer_key = Consumer_key
@client.oauth_config.consumer_secret = Consumer_secret
@client.oauth_config.signature_method = 'HMAC-SHA1'
@client.oauth_config.http_method = :get
@client.debug_dev = STDERR if $DEBUG
end
end
def base_url
default_port = (request.scheme == "http") ? 80 : 443
port = (request.port == default_port) ? "" : ":#{request.port.to_s}"
"#{request.scheme}://#{request.host}#{port}"
end
get '/' do
erb %{
<a href="/request_token">OAuth Login</a>
<a href="/activities">Activities</a>
}
end
get '/activities' do
# STILL DOES NOT WORK
content = @client.get_content("https://www.googleapis.com/buzz/v1/activities/@me/@consumption", :alt => :json, :prettyprint => true)
erb %{
<%= content %>
}
end
get '/request_token' do
res = @client.get_request_token(Request_token_url, Callback, :scope => Scope)
token = res.oauth_params['oauth_token']
secret = res.oauth_params['oauth_token_secret']
raise if token.nil? or secret.nil?
session[:request_token] = token
session[:request_token_secret] = secret
redirect "https://www.google.com/buzz/api/auth/OAuthAuthorizeToken?oauth_token=#{token}&domain=#{Consumer_key}&scope=#{Scope}&btmpl=mobile"
end
get '/access_token' do
token = params[:oauth_token]
session[:request_token] = token
secret = session[:request_token_secret]
verifier = params[:oauth_verifier]
res = @client.get_access_token(Access_token_url, token, secret, verifier)
# here, you can use valid @client,, BUT cannot re-incarnate it
content = @client.get_content("https://www.googleapis.com/buzz/v1/activities/@me/@consumption", :alt => :json, :prettyprint => true)
erb %{
<%= content %>
}
end
@kgbu

kgbu commented Jun 9, 2010

Copy link
Copy Markdown
Author

I'm not sure to re-use @client or not.
TODO
-re-initialize @client with session data, or persistent Buzz instance. (In fact, I'd like to make KeyRing class to manage various Services for each user.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment