简体   繁体   中英

OpenURI::HTTPError (500 Internal Server Error) with open-uri

I'm trying to download an image from an URL, the URL is from facebook. I get the URL after I signup with omniauth-facebook:

the steps are the following:

$ irb
>> require 'open-uri'
>> open("http://graph.facebook.com/v4.0/10224315028492218/picture?access_token=EAAPhFIKqb2IBAO6ekZAGnQyoqkxrmoBRAN8Qfc115SJkOzu5wOmCZB6C7IKtql3ZCIEw8NU66RXrmoFZAszU6G3VeXaz7KHZAmS2NVzF5uRKTEonQ43r1ZB8q1otb5MUmEW7YySXcINREet1xG8i1II5apMif97TAxpMoqDXwz9AZDZD")

and this is the result:

Traceback (most recent call last):
       13: from /home/abdel/.rbenv/versions/2.6.5/bin/irb:23:in `<main>'
       12: from /home/abdel/.rbenv/versions/2.6.5/bin/irb:23:in `load'
       11: from /home/abdel/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
       10: from (irb):8
        9: from (irb):8:in `rescue in irb_binding'
        8: from /home/abdel/.rbenv/versions/2.6.5/lib/ruby/2.6.0/open-uri.rb:35:in `open'
        7: from /home/abdel/.rbenv/versions/2.6.5/lib/ruby/2.6.0/open-uri.rb:736:in `open'
        6: from /home/abdel/.rbenv/versions/2.6.5/lib/ruby/2.6.0/open-uri.rb:165:in `open_uri'
        5: from /home/abdel/.rbenv/versions/2.6.5/lib/ruby/2.6.0/open-uri.rb:224:in `open_loop'
        4: from /home/abdel/.rbenv/versions/2.6.5/lib/ruby/2.6.0/open-uri.rb:224:in `catch'
        3: from /home/abdel/.rbenv/versions/2.6.5/lib/ruby/2.6.0/open-uri.rb:226:in `block in open_loop'
        2: from /home/abdel/.rbenv/versions/2.6.5/lib/ruby/2.6.0/open-uri.rb:756:in `buffer_open'
        1: from /home/abdel/.rbenv/versions/2.6.5/lib/ruby/2.6.0/open-uri.rb:378:in `open_http'
OpenURI::HTTPError (500 Internal Server Error)

trying the same but with https instead of just http:

Traceback (most recent call last):
       12: from /home/abdel/.rbenv/versions/2.6.5/bin/irb:23:in `<main>'
       11: from /home/abdel/.rbenv/versions/2.6.5/bin/irb:23:in `load'
       10: from /home/abdel/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
        9: from (irb):12
        8: from /home/abdel/.rbenv/versions/2.6.5/lib/ruby/2.6.0/open-uri.rb:35:in `open'
        7: from /home/abdel/.rbenv/versions/2.6.5/lib/ruby/2.6.0/open-uri.rb:736:in `open'
        6: from /home/abdel/.rbenv/versions/2.6.5/lib/ruby/2.6.0/open-uri.rb:165:in `open_uri'
        5: from /home/abdel/.rbenv/versions/2.6.5/lib/ruby/2.6.0/open-uri.rb:224:in `open_loop'
        4: from /home/abdel/.rbenv/versions/2.6.5/lib/ruby/2.6.0/open-uri.rb:224:in `catch'
        3: from /home/abdel/.rbenv/versions/2.6.5/lib/ruby/2.6.0/open-uri.rb:226:in `block in open_loop'
        2: from /home/abdel/.rbenv/versions/2.6.5/lib/ruby/2.6.0/open-uri.rb:756:in `buffer_open'
        1: from /home/abdel/.rbenv/versions/2.6.5/lib/ruby/2.6.0/open-uri.rb:378:in `open_http'
OpenURI::HTTPError (400 Bad Request)

putting the URL in the browser:

http://graph.facebook.com/v4.0/10224315028492218/picture?access_token=EAAPhFIKqb2IBAO6ekZAGnQyoqkxrmoBRAN8Qfc115SJkOzu5wOmCZB6C7IKtql3ZCIEw8NU66RXrmoFZAszU6G3VeXaz7KHZAmS2NVzF5uRKTEonQ43r1ZB8q1otb5MUmEW7YySXcINREet1xG8i1II5apMif97TAxpMoqDXwz9AZDZD

it's downloading it without problem, also when I try to use open-uri with another image link, it's working. I don't know why is it causing problem just with the image from facebook.

Solution

Thanks to the help of @Gowtham, I manage to download the image and use Active Storage to save it, this was my code:

url = "https://graph.facebook.com/v4.0/10224315028492218/picture?access_token=EAAPhFIKqb2IBAO6ekZAGnQyoqkxrmoBRAN8Qfc115SJkOzu5wOmCZB6C7IKtql3ZCIEw8NU66RXrmoFZAszU6G3VeXaz7KHZAmS2NVzF5uRKTEonQ43r1ZB8q1otb5MUmEW7YySXcINREet1xG8i1II5apMif97TAxpMoqDXwz9AZDZD"

begin
  file = File.open('user_avatar.jpeg', 'wb') do |file|
    file << open(url).read
  end
  user.avatar.attach(io: File.open(file), filename: 'user_avatar.jpeg')
rescue Exception => e
  p e.message, e.backtrace.inspect
end

Try the following with HTTPS URL

require 'open-uri'

url = "https://graph.facebook.com/v4.0/10224315028492218/picture?access_token=EAAPhFIKqb2IBAO6ekZAGnQyoqkxrmoBRAN8Qfc115SJkOzu5wOmCZB6C7IKtql3ZCIEw8NU66RXrmoFZAszU6G3VeXaz7KHZAmS2NVzF5uRKTEonQ43r1ZB8q1otb5MUmEW7YySXcINREet1xG8i1II5apMif97TAxpMoqDXwz9AZDZD"
begin
  File.open('test.jpeg', 'wb') do |file|
    file << open(url).read
  end
rescue Exception => e
  p e.message, e.backtrace.inspect
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM