Created
November 27, 2012 15:54
-
-
Save satoryu/4155009 to your computer and use it in GitHub Desktop.
Controller for authentication of Yammer API with OAuth2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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