How do I request a HTTPS page?

Discussion in 'Ruby' started by Samuel Sternhagen, Sep 6, 2010.

  1. I would like to access a https page from irb

    "https://www.etsy.com/oauth/signin?oauth_consumer_key=aaa&oauth_token=bbb"

    I have been unable to find an example to do this that works.

    Is there a good resource on how to do this with Ruby Net::HTTP ?

    Does anyone what the general process of this is?

    Thank you,

    Sam
    --
    Posted via http://www.ruby-forum.com/.
    Samuel Sternhagen, Sep 6, 2010
    #1
    1. Advertising

  2. Samuel Sternhagen wrote:
    > I would like to access a https page from irb
    >
    > "https://www.etsy.com/oauth/signin?oauth_consumer_key=aaa&oauth_token=bbb"
    >
    > I have been unable to find an example to do this that works.
    >
    > Is there a good resource on how to do this with Ruby Net::HTTP ?


    What platform are you on?

    If it's Ubuntu or Debian, note that you will be missing lib/net/https.rb
    unless you install the libopenssl-ruby package.

    From Lucid onwards you can install "ruby-full" which consolidates all
    the shrapnel (see below). Note that this will also install emacs as a
    dependency unless you do:

    sudo apt-get install --no-install-recommends ruby-full

    Regards,

    Brian.

    $ apt-cache show ruby-full
    Package: ruby-full
    Priority: optional
    Section: universe/interpreters
    Installed-Size: 32
    Maintainer: Ubuntu Core Developers
    <>
    Original-Maintainer: Fumitoshi UKAI <>
    Architecture: all
    Source: ruby-defaults
    Version: 4.2
    Depends: irb, libdbm-ruby, libgdbm-ruby, libopenssl-ruby,
    libreadline-ruby, rdoc, ri, ruby, ruby1.8-dev
    Recommends: libtcltk-ruby, ruby-elisp
    Filename: pool/universe/r/ruby-defaults/ruby-full_4.2_all.deb
    Size: 2580
    MD5sum: 2124a3d52a2300057e6b2662f770f6d7
    SHA1: 6701dd3238dd1a0657c66ff8a98336bf77426f75
    SHA256: 0d4c64bc60717dd27a2d6769b7dbbc0d0f0e65ca6bc2f6bc30c496bbb50380a6
    Description: Ruby full installation
    For many good reasons, the Ruby programming language is split in many
    small different packages. Installing this package will make sure you
    have
    all the packages that add up to a full Ruby installation, with the
    exception
    of the Tcl/Tk bindings for Ruby, which are only recommended.
    Brian Candler, Sep 7, 2010
    #2
    1. Advertising

  3. $ cat /etc/issue
    Ubuntu 10.04.1 LTS

    $ uname -a
    Linux oso 2.6.32-24-generic #42-Ubuntu SMP Fri Aug 20 14:21:58 UTC 2010
    x86_64 GNU/Linux

    $ apt-cache show ruby-full
    Package: ruby-full
    Priority: optional
    Section: universe/interpreters
    Installed-Size: 32
    Maintainer: Ubuntu Core Developers
    <>
    Original-Maintainer: Fumitoshi UKAI <>
    Architecture: all
    Source: ruby-defaults
    Version: 4.2
    Depends: irb, libdbm-ruby, libgdbm-ruby, libopenssl-ruby,
    libreadline-ruby, rdoc, ri, ruby, ruby1.8-dev
    Recommends: libtcltk-ruby, ruby-elisp
    Filename: pool/universe/r/ruby-defaults/ruby-full_4.2_all.deb
    Size: 2580
    MD5sum: 2124a3d52a2300057e6b2662f770f6d7
    SHA1: 6701dd3238dd1a0657c66ff8a98336bf77426f75
    SHA256: 0d4c64bc60717dd27a2d6769b7dbbc0d0f0e65ca6bc2f6bc30c496bbb50380a6
    --
    Posted via http://www.ruby-forum.com/.
    Samuel Sternhagen, Sep 8, 2010
    #3
  4. Samuel Sternhagen wrote:
    > $ cat /etc/issue
    > Ubuntu 10.04.1 LTS
    >
    > $ uname -a
    > Linux oso 2.6.32-24-generic #42-Ubuntu SMP Fri Aug 20 14:21:58 UTC 2010
    > x86_64 GNU/Linux
    >
    > $ apt-cache show ruby-full


    apt-cache shows what packages are available to install, not what are
    actually installed. Try:

    dpkg-query -l | grep ruby

    to see what you have. If you don't have libopenssl-ruby1.8 then do:

    sudo apt-get install ruby-full --no-recommends

    Then you can see what files you have:

    dpkg-query -L libopenssl-ruby1.8

    and this will include /usr/lib/ruby/1.8/net/https.rb
    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Sep 8, 2010
    #4
  5. $ dpkg-query -l | grep ruby
    ii libopenssl-ruby 4.2
    OpenSSL interface for Ruby
    ii libopenssl-ruby1.8 1.8.7.249-2
    OpenSSL interface for Ruby 1.8
    ii libreadline-ruby1.8 1.8.7.249-2
    Readline interface for Ruby 1.8
    ii libreadline-ruby1.9 1.9.0.5-1ubuntu2
    Readline interface for Ruby 1.9
    ii libruby1.8 1.8.7.249-2
    Libraries necessary to run Ruby 1.8
    ii libruby1.9 1.9.0.5-1ubuntu2
    Libraries necessary to run Ruby 1.9
    ii rake 0.8.7-1
    a ruby build program
    ii rdoc 4.2
    Generate documentation from ruby source file
    ii ruby 4.2
    An interpreter of object-oriented scripting
    ii ruby1.8 1.8.7.249-2
    Interpreter of object-oriented scripting lan
    ii ruby1.8-dev 1.8.7.249-2
    Header files for compiling extension modules
    ii ruby1.9 1.9.0.5-1ubuntu2
    Interpreter of object-oriented scripting lan
    ii rubygems 1.3.5-1ubuntu2
    package management framework for Ruby librar
    ii rubygems1.8 1.3.5-1ubuntu2
    package management framework for Ruby librar
    ii rubygems1.9 1.3.5-1ubuntu2
    package management framework for Ruby librar



    --
    Posted via http://www.ruby-forum.com/.
    Samuel Sternhagen, Sep 8, 2010
    #5
  6. Does

    require 'net/https'

    work in irb? Then look in /usr/lib/ruby/1.8/net/https.rb for the
    documentation, including sample code.
    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Sep 8, 2010
    #6
  7. Jesús Gabriel y Galán wrote:
    >
    > A simple google search for "ruby https net http" yields:
    >
    > http://snippets.dzone.com/posts/show/788
    >
    > maybe you can start from here and make some tests.
    > Or show us what you have tried and what are the errors?
    >
    > Jesus.


    First I will include my code and then 2 responses.

    require 'net/http'
    require 'net/https'
    require 'rubygems'
    require 'oauth'

    oauth_consumer_key = "xxx"
    oauth_consumer_secret = "yyy"


    @consumer = OAuth::Consumer.new(oauth_consumer_key,
    oauth_consumer_secret, {:site => "http://openapi.etsy.com",
    :eek:auth_callback => "oob", :request_token_path =>
    "/v2/sandbox/oauth/request_token", :authorize_path =>
    "/v2/sandbox/oauth/authorize", :access_token_path =>
    "/v2/sandbox/oauth/access_token" })


    @request_token = @consumer.get_request_token



    http = Net::HTTP.new('etsy.com', 443)
    http.use_ssl = true
    path =
    "oauth/signin?oauth_consumer_key=#{oauth_consumer_key}&oauth_token=#{@request_token.token}"



    resp = http.get(path)
    puts 'Code = ' + resp.code
    puts 'Message = ' + resp.message
    resp.each {|key, val| puts key + ' = ' + val}
    # done

    Here is the response the code yielded:

    $ ruby sec.rb
    warning: peer certificate won't be verified in this SSL session
    Code = 301
    Message = Moved Permanently
    location =
    http://www.etsy.com/oauth/signin?oauth_consumer_key=xxx&oauth_token=yyy
    expires = Thu, 19 Nov 1981 08:52:00 GMT
    connection = close
    content-type = text/html; charset=UTF-8
    server = Apache
    date = Wed, 08 Sep 2010 12:43:15 GMT
    content-length = 0
    cache-control = no-store, no-cache, must-revalidate, post-check=0,
    pre-check=0
    pragma = no-cache

    ###########################################################

    I then used the same code except I changed the path variable to include
    the 'https'

    path =
    "https://oauth/signin?oauth_consumer_key=#{oauth_consumer_key}&oauth_token=#{@request_token.token}"

    Then I received this response:

    $ ruby sec.rb
    warning: peer certificate won't be verified in this SSL session
    Code = 302
    Message = Found
    location =
    http://etsy.comhttps://etsy.com/oauth/signin?oauth_consumer_key=xxx&oauth_token=yyy
    connection = close
    server = BigIP
    content-length = 0

    #end

    In the second response the location is weird. It did however produce a
    "302".

    In the first response the location says: HTTP, but the url should be
    HTTPS
    --
    Posted via http://www.ruby-forum.com/.
    Samuel Sternhagen, Sep 8, 2010
    #7

  8. >
    > I then used the same code except I changed the path variable to include
    > the 'https'
    >
    > path =
    > "https://oauth/signin?oauth_consumer_key=#{oauth_consumer_key}&oauth_token=#{@request_token.token}"
    >

    Sorry, I should have wrote:

    path =
    "https://etsy.com/oauth/signin?oauth_consumer_key=#{oauth_consumer_key}&oauth_token=#{@request_token.token}"
    --
    Posted via http://www.ruby-forum.com/.
    Samuel Sternhagen, Sep 8, 2010
    #8
  9. Samuel Sternhagen wrote:
    > http = Net::HTTP.new('etsy.com', 443)


    The redirect says you should be using www.etsy.com, not etsy.com

    > http.use_ssl = true
    > path =
    > "oauth/signin?oauth_consumer_key=#{oauth_consumer_key}&oauth_token=#{@request_token.token}"


    Should have a leading slash on the path. (But not http:// or https://)

    If you still get a redirect, then maybe it really wants you to use http
    and not https. That is, it seems to be redirecting
    https://etsy.com/anyoldjunk to http://etsy.com/anyoldjunk
    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Sep 8, 2010
    #9
  10. Brian Candler wrote:
    > Does
    >
    > require 'net/https'
    >
    > work in irb? Then look in /usr/lib/ruby/1.8/net/https.rb for the
    > documentation, including sample code.


    Yeah, irb returns true when I require it. It seems like it only works in
    a certain order however. Usually it has to be second. I am not sure why.
    Thanks for the tip. I didn't realize that there is documentation there.
    --
    Posted via http://www.ruby-forum.com/.
    Samuel Sternhagen, Sep 8, 2010
    #10
  11. Brian Candler wrote:
    > Samuel Sternhagen wrote:
    >> http = Net::HTTP.new('etsy.com', 443)

    >
    > The redirect says you should be using www.etsy.com, not etsy.com
    >
    >> http.use_ssl = true
    >> path =
    >> "oauth/signin?oauth_consumer_key=#{oauth_consumer_key}&oauth_token=#{@request_token.token}"

    >
    > Should have a leading slash on the path. (But not http:// or https://)
    >
    > If you still get a redirect, then maybe it really wants you to use http
    > and not https. That is, it seems to be redirecting
    > https://etsy.com/anyoldjunk to http://etsy.com/anyoldjunk


    I added that leading '/' to the path variable. I have also added the www
    that you have suggested.

    You said that maybe it wants to used http so I tried changing the port
    number from 443 to 80.

    I get this:

    $ruby sec.rb
    warning: peer certificate won't be verified in this SSL session
    Code = 302
    Message = Moved Temporarily
    location =
    http://www.etsy.com/login.php?from_...uekbtj7w&oauth_token=agwt6cvhqv2ve5mvuy3fc7zs
    expires = Thu, 19 Nov 1981 08:52:00 GMT
    connection = close
    content-type = text/html; charset=UTF-8
    date = Wed, 08 Sep 2010 22:27:17 GMT
    server = Apache
    content-length = 26
    content-encoding = gzip
    cache-control = no-store, no-cache, must-revalidate, post-check=0,
    pre-check=0
    pragma = no-cache
    ��

    I am not sure what that last part is. I think it is some kind of
    hexidecimal code. Either way, that is what it returned.
    --
    Posted via http://www.ruby-forum.com/.
    Samuel Sternhagen, Sep 8, 2010
    #11
  12. Samuel Sternhagen wrote:
    > Brian Candler wrote:
    >> Does
    >>
    >> require 'net/https'
    >>
    >> work in irb? Then look in /usr/lib/ruby/1.8/net/https.rb for the
    >> documentation, including sample code.

    >
    > Yeah, irb returns true when I require it. It seems like it only works in
    > a certain order however


    What do you mean by "only works"? If it returns false, that is fine too.
    It simply means that the library has already been loaded. If it couldn't
    load it, it would raise an exception.
    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Sep 9, 2010
    #12
  13. Samuel Sternhagen wrote:
    > I added that leading '/' to the path variable. I have also added the www
    > that you have suggested.
    >
    > You said that maybe it wants to used http so I tried changing the port
    > number from 443 to 80.


    No, it must be 443 for https. For 80 (http) you would not say use_ssl =
    true.

    I suggest you keep it at 443 and use_ssl=true, and show what that
    response that gives.

    Show what it gives in that case. However I think this is no longer a
    ruby question, it's more of an oauth question, so you might get a better
    response in an oauth forum.

    > I am not sure what that last part is. I think it is some kind of
    > hexidecimal code. Either way, that is what it returned.


    Try using inspect, e.g.

    resp.each {|key, val| puts key.inspect + ' = ' + val.inspect}
    puts resp.body.inspect

    If you still get that junk, then it's coming from later on in your
    program.
    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Sep 9, 2010
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Birtle
    Replies:
    2
    Views:
    1,998
    John Saunders
    Oct 16, 2003
  2. =?Utf-8?B?cHBhdGVs?=

    Request.ServerVariables ("HTTP_REFERER") using https

    =?Utf-8?B?cHBhdGVs?=, Mar 2, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    3,530
    Eric Lawrence [MSFT]
    Mar 3, 2004
  3. Axel
    Replies:
    8
    Views:
    1,067
    Adrienne Boswell
    Apr 27, 2009
  4. jotto
    Replies:
    4
    Views:
    375
    jotto
    Oct 2, 2006
  5. Naveen Dhanuka
    Replies:
    1
    Views:
    266
Loading...

Share This Page