require 'faraday'

module Faraday
  class Request::OAuth2 < Faraday::Middleware
    begin
      require 'oauth2'
    rescue LoadError, NameError => error
      self.load_error = error
    end

    def call(env)
      params = env[:url].query_values || {}
      env[:url].query_values = params.merge('access_token' => @token)
      env[:request_headers].merge!('Authorization' => "Token token=\"#{@token}\"")

      @app.call env
    end

    def initialize(app, *args)
      @app = app
      @token = args.shift
    end
  end
end

require 'faraday'

module Faraday
  class Request::OAuth2 < Faraday::Middleware
    begin
      require 'oauth2'
    rescue LoadError, NameError => error
      self.load_error = error
    end

    def call(env)
      params = env[:url].query_values || {}

      env[:url].query_values = { 'access_token' => @token }.merge(params)

      token = env[:url].query_values['access_token']

      env[:request_headers].merge!('Authorization' => "Token token=\"#{token}\"")

      @app.call env
    end

    def initialize(app, *args)
      @app = app
      @token = args.shift
    end
  end
end

Allow for the token to be set either in the middleware setup or in the query values during connection execution.
Allow for the token to be set either in the middleware setup or in the query values during connection execution.