Skip to content

Instantly share code, notes, and snippets.

@runeb
Created January 12, 2010 12:30
Show Gist options
  • Save runeb/275153 to your computer and use it in GitHub Desktop.
Save runeb/275153 to your computer and use it in GitHub Desktop.
# Heroku .gems file
oauth
nokogiri
class ApplicationController < ActionController::Base
protected
def initialize_user
# If we have oauth_token and oauth_secret, we
# can recreate the access token needed for
# YouTube API communication on behalf of the
# user
if session[:oauth_token] && session[:oauth_secret]
@access_token ||= OAuth::AccessToken.new(
consumer,
session[:oauth_token],
session[:oauth_secret])
end
end
# This is the OAuth::Consumer we use to communicate
# with Google
def consumer
@consumer ||= begin
options = {
:site => "https://www.google.com",
:request_token_path => "/accounts/OAuthGetRequestToken",
:access_token_path => "/accounts/OAuthGetAccessToken",
:authorize_path=> "/accounts/OAuthAuthorizeToken"
}
OAuth::Consumer.new('YOUR KEY', 'YOUR SECRET', options)
end
end
end
RAILS_GEM_VERSION = '2.3.5' unless defined? RAILS_GEM_VERSION
require File.join(File.dirname(__FILE__), 'boot')
Rails::Initializer.run do |config|
config.gem 'oauth'
config.gem 'nokogiri'
config.time_zone = 'UTC'
end
class HomepageController < ApplicationController
before_filter :initialize_user
def index
# If we have the instance vaiable @access_token, then
# we must assume we can use this to communicate with
# the YouTube API on behalf of the logged in user.
#
# Here we simply fetch her username so we can display
# it in the view
if @access_token
body = @access_token.get("http://gdata.youtube.com/feeds/api/users/default").body
@username = Nokogiri::XML.parse(body).search("author").children.first.text
end
end
end
<h1>YouTube OAuth example</h1>
<% if @username %>
<p>You are logged in to this site via YouTube as <%= @username %>!</p>
<br><br>
<p><%= link_to 'Log out', :controller => :session, :action => :destroy %></p>
<% else %>
<%= link_to 'Log in via YouTube', :controller => :session, :action => :new %>
<% end %>
ActionController::Routing::Routes.draw do |map|
map.root :controller => :homepage
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end
class SessionController < ApplicationController
def new
request_token = consumer.get_request_token(
{:oauth_callback => "http://runeb-oauth.heroku.com/session/create" },
{:scope => "http://gdata.youtube.com"})
# Keep the secret
session[:oauth_secret] = request_token.secret
# Redirect to Google for authorization
redirect_to request_token.authorize_url
end
# User authorized us at Google site
def create
# Recreate the (now authorized) request token
request_token = OAuth::RequestToken.new(consumer,
params[:oauth_token],
session[:oauth_secret])
# Swap the authorized request token for an access token
access_token = request_token.get_access_token(
{:oauth_verifier => params[:oauth_verifier]})
# Save the token and secret to the session
# We use these to recreate the access token
session[:oauth_token] = access_token.token
session[:oauth_secret] = access_token.secret
redirect_to "/"
end
def destroy
# User logs out, forget the access token
reset_session
redirect_to '/'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment