Wynn Netherland changelog.com/posts

OAuth2: Ruby wrapper for OAuth 2.0

OAuth 2.0, which aims to simplify OAuth flow, is still a work in progress, but wrapper libraries are already shaping up thanks to interest in Facebook’s support.

Michael Bleigh’s gem provides a nice Ruby wrapper for OAuth2.

sudo gem install oauth2

… and use it like so:

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

def client
  OAuth2::Client.new('api_key', 'api_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.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

[Source on GitHub]


Discussion

Sign in or Join to comment or subscribe

Player art
  0:00 / 0:00