def login_by_line(server = 'http://localhost/oauth2callback', port = 0)
      begin
        require "launchy" # open browser
      rescue
        raise GoogleApi::RequireError, "You don't have launchy gem. Firt install it: gem install launchy."
      end
      
      require "socket"  # make tcp server 
      require "uri"     # parse uri

      uri = URI(server)

      # Start webserver.
      webserver = TCPServer.new(uri.host, port)

      # By default port is 0. It means that TCPServer will get first free port.
      # Port is required for redirect_uri.
      uri.port = webserver.addr[1]

      # Add redirect_uri for google oauth 2 callback.
      _config.send(@config_name).redirect_uri = uri.to_s

      # Open browser.
      Launchy.open(login)

      # Wait for new session.
      session = webserver.accept

      # Parse header for query.
      request = session.gets.gsub(/GET\ \//, '').gsub(/\ HTTP.*/, '')
      request = Hash[URI.decode_www_form(URI(request).query)]

      # Failure login
      to_return = false
      message   = "You have not been logged. Please try again."

      if login(request['code'])
        message = "You have been successfully logged. Now you can close the browser."

        to_return = true
      end

      session.write(message)

      # Close session and webserver.
      session.close

      return to_return
    end
def login_by_line(server = 'http://localhost/oauth2callback', port = 0)
      begin
        require "launchy" # open browser
      rescue
        raise GoogleApi::RequireError, "You don't have launchy gem. Firt install it: gem install launchy."
      end
      
      require "socket"  # make tcp server 
      require "uri"     # parse uri

      uri = URI(server)

      # Start webserver.
      webserver = TCPServer.new(uri.host, port)

      # By default port is 0. It means that TCPServer will get first free port.
      # Port is required for redirect_uri.
      uri.port = webserver.addr[1]

      # Add redirect_uri for google oauth 2 callback.
      _config.send(@config_name).redirect_uri = uri.to_s

      # Open browser.
      Launchy.open(login)

      # Wait for new session.
      session = webserver.accept

      # Parse header for query.
      request = session.gets.gsub(/GET\ \//, '').gsub(/\ HTTP.*/, '')
      request = Hash[URI.decode_www_form(URI(request).query)]

      # Failure login
      to_return = false
      message   = "You have not been logged. Please try again."

      if login(request['code'])
        message = "You have been successfully logged. Now you can close the browser."

        to_return = true
      end

      session.write(message)

      # Close session and webserver.
      session.close

      return to_return
    end
Automaticaly open autorization url a waiting for callback.
 Launchy gem is required

 Parameters:
   server:: server will be on this addres, its alson address for oatuh 2 callback
   port:: listening port for server
   port=0:: server will be on first free port

 Steps:
 1) create server
 2) launch browser and redirect to google api
 3) confirm and google api redirect to localhost
 4) server get code and start session
 5) close server - you are login