Skip to content
Browse files

Merge branch 'master' of https://github.com/jamiew/omniauth

  • Loading branch information...
2 parents cf4d81c + d555a77 commit 4aeed818895a2066649cb975fa77037a9d776ad4 Michael Bleigh committed Jan 30, 2011
View
4 oa-oauth/lib/omniauth/oauth.rb
@@ -4,7 +4,7 @@ module OmniAuth
module Strategies
autoload :OAuth, 'omniauth/strategies/oauth'
autoload :OAuth2, 'omniauth/strategies/oauth2'
-
+
autoload :Twitter, 'omniauth/strategies/twitter'
autoload :LinkedIn, 'omniauth/strategies/linked_in'
autoload :Facebook, 'omniauth/strategies/facebook'
@@ -24,5 +24,7 @@ module Strategies
autoload :Google, 'omniauth/strategies/google'
autoload :Netflix, 'omniauth/strategies/netflix'
autoload :Bitly, 'omniauth/strategies/bitly'
+ autoload :Vimeo, 'omniauth/strategies/vimeo'
+ autoload :YouTube, 'omniauth/strategies/you_tube'
end
end
View
54 oa-oauth/lib/omniauth/strategies/vimeo.rb
@@ -0,0 +1,54 @@
+require 'omniauth/oauth'
+require 'multi_json'
+
+module OmniAuth
+ module Strategies
+ #
+ # Authenticate to Vimeo via OAuth and retrieve basic user information.
+ #
+ # Usage:
+ #
+ # use OmniAuth::Strategies::Vimeo, 'consumerkey', 'consumersecret'
+ #
+ class Vimeo < OmniAuth::Strategies::OAuth
+ def initialize(app, consumer_key = nil, consumer_secret = nil, options = {}, &block)
+ opts = {
+ :site => 'http://vimeo.com',
+ :request_token_path => '/oauth/request_token',
+ :access_token_path => '/oauth/access_token',
+ :authorize_path => '/oauth/authorize'
+ }
+ super(app, :vimeo, consumer_key, consumer_secret, opts, options, &block)
+ end
+
+ def auth_hash
+ user = user_hash['person']
+ OmniAuth::Utils.deep_merge(super, {
+ 'uid' => user['id'],
+ 'user_info' => user_info,
+ 'extra' => { 'user_hash' => user }
+ })
+ end
+
+ def user_info
+ user = user_hash['person']
+ {
+ 'nickname' => user['username'],
+ 'name' => user['display_name'],
+ 'location' => user['location'],
+ 'description' => user['bio'],
+ 'image' => user['portraits']['portrait'].select{|h| h['height'] == '300'}.first['_content'],
+ 'urls' => {
+ 'website' => user['url'],
+ 'vimeo' => user['profileurl']
+ }
+ }
+ end
+
+ def user_hash
+ url = "http://vimeo.com/api/rest/v2?method=vimeo.people.getInfo&format=json"
+ @user_hash ||= MultiJson.decode(@access_token.get(url).body)
+ end
+ end
+ end
+end
View
73 oa-oauth/lib/omniauth/strategies/you_tube.rb
@@ -0,0 +1,73 @@
+# Based heavily on the Google strategy, monkeypatch and all
+
+require 'omniauth/oauth'
+require 'multi_json'
+
+module OmniAuth
+ module Strategies
+ #
+ # Authenticate to YouTube via OAuth and retrieve basic user info.
+ #
+ # Usage:
+ #
+ # use OmniAuth::Strategies::YouTube, 'consumerkey', 'consumersecret'
+ #
+ class YouTube < OmniAuth::Strategies::OAuth
+ def initialize(app, consumer_key = nil, consumer_secret = nil, options = {}, &block)
+ client_options = {
+ :site => 'https://www.google.com',
+ :request_token_path => '/accounts/OAuthGetRequestToken',
+ :access_token_path => '/accounts/OAuthGetAccessToken',
+ :authorize_path => '/accounts/OAuthAuthorizeToken'
+ }
+
+ super(app, :you_tube, consumer_key, consumer_secret, client_options, options)
+ end
+
+ def auth_hash
+ ui = user_info
+ OmniAuth::Utils.deep_merge(super, {
+ 'uid' => ui['uid'],
+ 'user_info' => ui,
+ 'extra' => {'user_hash' => user_hash}
+ })
+ end
+
+ def user_info
+ entry = user_hash['entry']
+ {
+ 'uid' => entry['id']['$t'],
+ 'nickname' => entry['author'].first['name']['$t'],
+ 'first_name' => entry['yt$firstName'] && entry['yt$firstName']['$t'],
+ 'last_name' => entry['yt$lastName'] && entry['yt$lastName']['$t'],
+ 'image' => entry['media$thumbnail'] && entry['media$thumbnail']['url'],
+ 'description' => entry['yt$description'] && entry['yt$description']['$t'],
+ 'location' => entry['yt$location'] && entry['yt$location']['$t']
+ }
+ end
+
+ def user_hash
+ # YouTube treats 'default' as the currently logged-in user
+ # via http://apiblog.youtube.com/2010/11/update-to-clientlogin-url.html
+ @user_hash ||= MultiJson.decode(@access_token.get("http://gdata.youtube.com/feeds/api/users/default?alt=json").body)
+ end
+
+ # Monkeypatch consumer.get_request_token but specify YouTube scope rather than Google Contacts
+ # TODO this is an easy patch to the underlying OAuth strategy a la OAuth2
+ def request_phase
+ request_token = consumer.get_request_token({:oauth_callback => callback_url}, {:scope => 'http://gdata.youtube.com'})
+
+ (session['oauth']||={})[name.to_s] = {'callback_confirmed' => request_token.callback_confirmed?, 'request_token' => request_token.token, 'request_secret' => request_token.secret}
+ r = Rack::Response.new
+
+ if request_token.callback_confirmed?
+ r.redirect(request_token.authorize_url)
+ else
+ r.redirect(request_token.authorize_url(:oauth_callback => callback_url))
+ end
+
+ r.finish
+ end
+ end
+ end
+end
View
5 oa-oauth/spec/omniauth/strategies/vimeo_spec.rb
@@ -0,0 +1,5 @@
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
+
+describe OmniAuth::Strategies::Vimeo do
+ it_should_behave_like 'an oauth strategy'
+end
View
5 oa-oauth/spec/omniauth/strategies/you_tube_spec.rb
@@ -0,0 +1,5 @@
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
+
+describe OmniAuth::Strategies::YouTube do
+ it_should_behave_like 'an oauth strategy'
+end

0 comments on commit 4aeed81

Please sign in to comment.
Something went wrong with that request. Please try again.