Project

nov-oauth2

0.0
No commit activity in last 3 years
No release in over 3 years
A Ruby wrapper for the OAuth 2.0 protocol built with a similar style to the original OAuth gem.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.4.6
~> 0.8
~> 0.9
~> 2.4.0

Runtime

~> 0.5.0
~> 0.0.4
 Project Readme

OAuth2¶ ↑

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.

Installation¶ ↑

gem install oauth2

Resources¶ ↑

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
  OAuth2::Client.new('app_id', 'app_secret', :site => 'https://graph.facebook.com')
end

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

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'))

  user.inspect
end

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

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 (wiki.github.com/intridea/oauth2/examples)

JSON Parsing¶ ↑

Because JSON has become the standard format of the OAuth 2.0 specification, the oauth2 gem contains a mode that will perform automatic parsing of JSON response bodies, returning a hash instead of a string. To enable this mode, simply add the :parse_json option to your client initialization:

client = OAuth2::Client.new('app_id', 'app_secret', 
  :site => 'https://example.com', 
  :parse_json => true
)

# Obtain an access token using the client
token.get('/some/url.json') # {"some" => "hash"}

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.