Find file
Fetching contributors…
Cannot retrieve contributors at this time
64 lines (45 sloc) 2.13 KB


A Ruby wrapper for the OAuth 2.0 specification. This is a work in progress, being built first to solve the pragmatic process of connecting to existing OAuth 2.0 endpoints (a.k.a. Facebook) with the goal of building it up to meet the entire specification over time.


gem install oauth2


Web Server Example (Sinatra)

Below is a fully functional example of a Sinatra application that would authenticate to Facebook utilizing the OAuth 2.0 web server flow.

require 'rubygems'
require 'sinatra'
require 'oauth2'
require 'json'

def client'app_id', 'app_secret', :site => '')

get '/auth/facebook' do
  redirect client.web_server.authorize_url(
    :redirect_uri => redirect_uri, 
    :scope => 'email,offline_access'

get '/auth/facebook/callback' do
  access_token = client.web_server.get_access_token(params[:code], :redirect_uri => redirect_uri)
  user = JSON.parse(access_token.get('/me'))


def redirect_uri
  uri = URI.parse(request.url)
  uri.path = '/auth/facebook/callback'
  uri.query = nil

That's all there is to it! You can use the access token like you would with the OAuth gem, calling HTTP verbs on it etc. You can view more examples on the OAuth2 Wiki (

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don't break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.


Copyright © 2010 Intridea, Inc. and Michael Bleigh. See LICENSE for details.