Code Monkey home page Code Monkey logo

omniauth-twitter2's Introduction

OmniAuth::Twitter2

test GitHub license Gem Version

This gem provides a OmniAuth strategy for authenticating with Twitter OAuth2.

Installation

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

Usage

Rails

# 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"
end

Auth Hash

  { "provider" => "twitter2",
    "uid" => "108252390",
    "info" => {
      "name" => "うなすけ",
      "email" => nil,
      "nickname" => "yu_suke1994",
      "description" => "帰って寝たい",
      "image" => "https://pbs.twimg.com/profile_images/580019517608218624/KzEZSzUy_normal.jpg",
      "urls" => {
        "Website" => "https://t.co/NCFLB8wDkx",
        "Twitter" => "https://twitter.com/yu_suke1994"
      }
    },
    "credentials" => {
      "token" => "TOKENTOKENTOKENTOKENTOKENTOKEN",
      "expires_at" => 1642016242,
      "expires" => true
    },
    "extra" => {
      "raw_info" => {
        "data" => {
          "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"
        }
      }
    }
  }

Development

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.

References

Sample App

Contributing

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.

License

The gem is available as open source under the terms of the Apache License 2.0.

Code of Conduct

Everyone interacting in the OmniAuth::Twitter2 project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

omniauth-twitter2's People

Contributors

unasuke avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

omniauth-twitter2's Issues

unauthorized_client: Missing valid authorization header

Hi,

When used with offline_access scope. Getting a new token with the refresh_token fails with unauthorized_client: Missing valid authorization header

  def refresh
    strategy = OmniAuth::Strategies::Twitter2.new(nil, Rails.application.credentials.twitter[:client_id], Rails.application.credentials.twitter[:client_secret])
    client = strategy.client

    token = OAuth2::AccessToken.new client, nil, {refresh_token: self.refresh_token}
    new_token = token.refresh!
  end

Regards

PKCE strategy

I see there is a PKCE strategy.

How does that get run?

Twitter OAuth2 flow always asks for user authorization confirmation

I noticed the OAuth2 flow always asks users to confirm authorization when signing in, even after they've already given authorization to the app before.

From the Twitter docs all I can see that somewhat resembles what I want to do (once the user account gives authorization don't request confirmation next time until it was revoked by the user, like in OAuth 1.0) is offline.access.

I added this scope to the basic "tweet.read users.read", but from what I've tested I now believe that this only works on access tokens. Every time a user is signed out from the web app where I'm implementing this gem (migrate from 1.0 to OAuth 2) or wants to sign in on a different device/browser they will still go through the OAuth flow and Twitter will request authorization confirmation.

Is there a way to get around this? Perhaps I'm overlooking something.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.