Skip to content

Instantly share code, notes, and snippets.

@satoryu
Created November 27, 2012 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satoryu/4155009 to your computer and use it in GitHub Desktop.
Save satoryu/4155009 to your computer and use it in GitHub Desktop.
Controller for authentication of Yammer API with OAuth2
class ApplicationController < ActionController::Base
protect_from_forgery
def user
session[:current_user]
end
def logined?
not session[:current_user].nil?
end
def oauth2_client
@client ||= OAuth2::Client.new(ENV['CLIENT_ID'], ENV['CLIENT_SECRET'],
:site => 'https://www.yammer.com',
:authorize_url => '/dialog/oauth',
:token_url => '/oauth2/access_token.json',
:mode => :query,
:token_method => :get)
end
end
class AuthController < ApplicationController
def login
return if logined?
return redirect_to oauth2_client.auth_code.authorize_url(:redirect_uri => callback_url)
end
def logout
session[:current_user] = nil
redirect_to welcome_path
end
def callback
token = oauth2_client.auth_code.get_token(params[:code])
session[:current_user] = token['user']['id']
access_token = eval(token.token)['token']
token = OAuth2::AccessToken.new(token.client, access_token)
token.post "api/v1/messages.json", :body => { :body => "This is test." }
redirect_to welcome_path
end
end
YammerApp::Application.routes.draw do
root :to => "welcome#index", :as => :welcome
get 'login' => "auth#login", :as => :login
get 'logout' => "auth#logout", :as => :logout
get 'callback' => "auth#callback", :as => :callback
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment