Class: OmniAuth::Strategies::Twitter

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

Overview

Authenticate to Twitter via OAuth and retrieve basic user information.

Usage: use OmniAuth::Strategies::Twitter, '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, #request_phase, #unique_id

Constructor Details

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

Initialize the middleware

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :sign_in (Boolean, true)

    When true, use the "Sign in with Twitter" flow instead of the authorization flow.



15
16
17
18
19
20
21
22
# File 'lib/omniauth/strategies/oauth/twitter.rb', line 15

def initialize(app, consumer_key=nil, consumer_secret=nil, options={}, &block)
  client_options = {
    :site => 'https://api.twitter.com',
  }
  options[:authorize_params] = {:force_login => 'true'} if options.delete(:force_login) == true
  client_options[:authorize_path] = '/oauth/authenticate' unless options[:sign_in] == false
  super(app, options[:name] || :twitter, consumer_key, consumer_secret, client_options, options, &block)
end

Instance Method Details

#auth_hashObject



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/omniauth/strategies/oauth/twitter.rb', line 24

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

#user_hashObject



51
52
53
54
55
# File 'lib/omniauth/strategies/oauth/twitter.rb', line 51

def user_hash
  @user_hash ||= MultiJson.decode(@access_token.get('/1/account/verify_credentials.json').body)
rescue ::Errno::ETIMEDOUT
  raise ::Timeout::Error
end

#user_infoObject



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

def 
  user_hash = self.user_hash
  {
    'nickname' => user_hash['screen_name'],
    'name' => user_hash['name'] || user_hash['screen_name'],
    'location' => user_hash['location'],
    'image' => user_hash['profile_image_url'],
    'description' => user_hash['description'],
    'urls' => {
      'Website' => user_hash['url'],
      'Twitter' => 'http://twitter.com/' + user_hash['screen_name'],
    },
  }
end