Created
November 28, 2013 22:38
-
-
Save matthewrobertson/7699048 to your computer and use it in GitHub Desktop.
generate oauth tokens
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
require 'oauth' | |
require 'forwardable' | |
module Sredder | |
WRIKE_OAUTH_OPTIONS = { | |
:site => 'https://www.wrike.com', | |
:authorize_path => '/rest/auth/authorize', | |
:access_token_path => '/rest/auth/access_token', | |
:request_token_path => '/rest/auth/request_token' | |
} | |
class WrikeAuth | |
extend Forwardable | |
attr_accessor :sredderc | |
attr_writer :consumer | |
def_delegators :sredderc, :credentials, :credentials= | |
def initialize(sredderc = Sredderc.new) | |
@sredderc = sredderc | |
@sredderc.load | |
end | |
def run_oauth_procedure | |
@request_token = consumer.get_request_token | |
puts 'Sredder is about to open your browser to complete the oauth protocol.' | |
puts 'Please return to the terminal after you authenticate with Wrike.' | |
Util.wait_for_input | |
`open #{@request_token.authorize_url}` | |
Util.wait_for_input | |
store_tokens(@request_token.get_access_token) | |
end | |
def authorized? | |
!!credentials[:wrike_token] && !!credentials[:wrike_secret] | |
end | |
def oauth_access_token | |
if @access_token || authorized? | |
@access_token ||= OAuth::AccessToken.new(consumer, credentials[:wrike_token], credentials[:wrike_secret]) | |
end | |
end | |
private | |
def consumer | |
@consumer ||= OAuth::Consumer.new(key, oauth_secret, WRIKE_OAUTH_OPTIONS) | |
end | |
def key | |
end | |
def oauth_secret | |
end | |
def store_tokens(access_token) | |
@access_token = access_token | |
self.credentials[:wrike_secret] = access_token.secret | |
self.credentials[:wrike_token] = access_token.token | |
sredderc.save | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment