Class: OmniAuth::Strategies::YouTube

Inherits:
OAuth
  • Object
show all
Defined in:
lib/omniauth/strategies/you_tube.rb

Overview

Authenticate to YouTube via OAuth and retrieve basic user info.

Usage:

use OmniAuth::Strategies::YouTube, 'consumerkey', 'consumersecret'

Instance Attribute Summary

Attributes inherited from OAuth

#consumer_key, #consumer_options, #consumer_secret, #name

Instance Method Summary collapse

Methods inherited from OAuth

#callback_phase, #consumer, #unique_id

Constructor Details

#initialize(app, consumer_key = nil, consumer_secret = nil, options = {}, &block) ⇒ YouTube

Returns a new instance of YouTube.



16
17
18
19
20
21
22
23
24
25
# File 'lib/omniauth/strategies/you_tube.rb', line 16

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

Instance Method Details

#auth_hashObject



27
28
29
30
31
32
33
34
# File 'lib/omniauth/strategies/you_tube.rb', line 27

def auth_hash
  ui = 
  OmniAuth::Utils.deep_merge(super, {
    'uid' => ui['uid'],
    'user_info' => ui,
    'extra' => {'user_hash' => user_hash}
  })
end

#request_phaseObject

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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/omniauth/strategies/you_tube.rb', line 57

def request_phase
  request_token = consumer.get_request_token({:oauth_callback => callback_url}, {:scope => 'http://gdata.youtube.com'})
  session['oauth'] ||= {}
  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

#user_hashObject



49
50
51
52
53
# File 'lib/omniauth/strategies/you_tube.rb', line 49

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

#user_infoObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/omniauth/strategies/you_tube.rb', line 36

def 
  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