Skip to content

Instantly share code, notes, and snippets.

@ashaw
Created July 21, 2010 17:32
Show Gist options
  • Save ashaw/484812 to your computer and use it in GitHub Desktop.
Save ashaw/484812 to your computer and use it in GitHub Desktop.
module Typepad
class Auth
AUTH_KEYS = File.open(File.dirname(__FILE__) + '/' + 'typepad_oauth.yml') {|y| YAML::load(y)}
CONSUMER_KEY = AUTH_KEYS['consumer_key'].to_s
CONSUMER_SECRET = AUTH_KEYS['consumer_secret'].to_s
#owner_id is application id in settings
OWNER_ID = AUTH_KEYS['owner_id'].to_s
def initialize
@consumer = OAuth::Consumer.new(CONSUMER_KEY,CONSUMER_SECRET, {
:site => "https://www.typepad.com",
:scheme => :query_string,
:http_method => :get,
:request_token_path => "/secure/services/oauth/request_token",
:access_token_path => "/secure/services/oauth/access_token",
:authorize_path => "/secure/services/api/#{OWNER_ID}/oauth-approve"
})
@[email protected]_request_token :oauth_callback => 'http://127.0.0.1:4567/'
end
def self.parse_authenticated_endpoint(token,access,endpoint)
consumer = OAuth::Consumer.new(CONSUMER_KEY,CONSUMER_SECRET, {
:site => "https://api.typepad.com",
:scheme => :header,
:http_method => :post
})
access_token = OAuth::AccessToken.new(consumer, token, access)
f = access_token.get("#{endpoint}.json").body
data = Crack::JSON.parse(f)
end
def get_tokens
#store @request_token.token and @request_token.secret for use when you get the callback
#ask user to visit @request_token.authorize_url
@tokens = {}
@tokens[:request_token] = @request_token.token
@tokens[:secret] = @request_token.secret
@tokens[:url] = @request_token.authorize_url
@tokens
end
def access_token(oauth_token,secret,oauth_verifier)
puts oauth_token
puts secret
#... in your callback page:
# request_token_key will be 'oauth_token' in the query paramaters of the incoming get request
@request_token = OAuth::RequestToken.new(@consumer, oauth_token, secret)
@access_token=@request_token.get_access_token :oauth_verifier => oauth_verifier
@access_tokens = {}
@access_tokens[:token] = @access_token.token
@access_tokens[:secret] = @access_token.secret
@access_tokens
#store @access_token.token and @access_token.secret
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment