This gem provides a OmniAuth strategy for authenticating with Twitter OAuth2.
As of April 2025, Twitter/X API v2 supports returning the user's email address via the confirmed_email field. See the X Developer Community announcement.
To request the email, include the users.email scope:
scope: "tweet.read users.read users.email"Note: The email will only be returned if:
- Your Twitter app has "Request email from users" enabled in the Developer Portal
- The user has a confirmed email address on their Twitter account
- The user grants permission during OAuth
If these conditions aren't met, email will be nil in the auth hash.
Add this line to your application's Gemfile:
gem 'omniauth-twitter2'And then execute:
$ bundle install
Or install it yourself as:
$ gem install omniauth-twitter2
# config/initializers/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter2, ENV["TWITTER_CLIENT_ID"], ENV["TWITTER_CLIENT_SECRET"], callback_path: '/auth/twitter2/callback', scope: "tweet.read users.read users.email"
end { "provider" => "twitter2",
"uid" => "108252390",
"info" => {
"name" => "うなすけ",
"email" => "user@example.com", # nil if users.email scope not granted or email not confirmed
"nickname" => "yu_suke1994",
"description" => "帰って寝たい",
"image" => "https://pbs.twimg.com/profile_images/580019517608218624/KzEZSzUy_normal.jpg",
"urls" => {
"Website" => "https://t.co/NCFLB8wDkx",
"Twitter" => "https://x.com/yu_suke1994"
}
},
"credentials" => {
"token" => "TOKENTOKENTOKENTOKENTOKENTOKEN",
"expires_at" => 1642016242,
"expires" => true
},
"extra" => {
"raw_info" => {
"data" => {
"confirmed_email" => "user@example.com", # only present if users.email scope granted
"profile_image_url" => "https://pbs.twimg.com/profile_images/580019517608218624/KzEZSzUy_normal.jpg",
"url" => "https://t.co/NCFLB8wDkx",
"public_metrics" => {
"followers_count" => 2188,
"following_count" => 1478,
"tweet_count" => 162937,
"listed_count" => 110
},
"verified" => false,
"name" => "うなすけ",
"entities" => {
"url" => {
"urls" => [{
"start" => 0,
"end" => 23,
"url" => "https://t.co/NCFLB8wDkx",
"expanded_url" => "https://unasuke.com", "display_url" => "unasuke.com"
}]
}
},
"description" => "帰って寝たい",
"created_at" => "2010-01-25T10:10:22.000Z",
"username" => "yu_suke1994",
"protected" => false,
"id" => "108252390"
}
}
}
}After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.
- Twitter official resources
- arunagw/omniauth-twitter: OmniAuth strategy for Twitter
- omniauth/omniauth-oauth2: An abstract OAuth2 strategy for OmniAuth.
- nov/twitter_oauth2: Twitter OAuth 2.0 Client Library in Ruby
Bug reports and pull requests are welcome on GitHub at https://github.com/unasuke/omniauth-twitter2. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
The gem is available as open source under the terms of the Apache License 2.0.
Everyone interacting in the OmniAuth::Twitter2 project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.