Last active
March 31, 2016 09:43
-
-
Save LolWalid/fb9242e65086119cd147 to your computer and use it in GitHub Desktop.
How to init oauth - Rails
This file contains hidden or 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 Oauth | |
def client | |
consumer = OAuth::Consumer.new( | |
consumer_key, consumer_secret, site: BASE_URL) | |
token = { | |
oauth_token: access_token, | |
oauth_token_secret: access_token_secret | |
} | |
OAuth::AccessToken.from_hash(consumer, token) | |
end | |
end | |
# client.get(endpoint) |
This file contains hidden or 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 OauthController < ApplicationController | |
# the connect button in your website | |
def connect | |
oauth = OAuth::Consumer.new('consumer_key', 'consumer_secret', site: 'http://websiteyourequest.com') | |
@request_token = oauth.get_request_token(oauth_callback: 'your_callback_url') | |
session[:app_token] = @request_token.token | |
session[:app_secret] = @request_token.secret | |
redirect_to @request_token.authorize_url | |
end | |
def oauth | |
unless params[:denied].present? | |
oauth = OAuth::Consumer.new('consumer_key', 'consumer_secret', site: 'http://websiteyourequest.com') | |
request_token = OAuth::RequestToken.new(oauth, session[:app_token], session[:app_secret]) | |
access_token = request_token.get_access_token(oauth_verifier: params[:oauth_verifier]) | |
# token are stored in access_token.params | |
puts access_token.params | |
end | |
redirect_to root_path | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment