Skip to content

Instantly share code, notes, and snippets.

@matthewrobertson
Created November 28, 2013 22:38
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 matthewrobertson/7699048 to your computer and use it in GitHub Desktop.
Save matthewrobertson/7699048 to your computer and use it in GitHub Desktop.
generate oauth tokens
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