如何在Rails中获取302重定向位置? (已尝试过 HTTParty、Net/Http 和 RedirectFollower)

发布于 2024-10-06 11:12:54 字数 990 浏览 1 评论 0原文

你好
我正在尝试获取 Facebook 用户相册的封面图片。
正如 API 页面中所述,它在获取时返回“带有专辑封面图片 URL 的 HTTP 302”:
http s://graph.facebook.com/[album_id]}/picture?access_token=blahblahblah...
此处的文档: http://developers.facebook.com/docs/reference/api/album< /a>

我尝试过 HTTParty、Net:HTTP 以及 RedirectFollower 类
HTTParty 返回图片本身,并且任何地方都没有“位置”(URL)信息
NET:HTTP 和 RedirectFollower 有点棘手......
如果我在将 URL 传递到 get 方法时不使用 URI.encode,则会导致“bad uri”错误
但如果我使用 URI.encode 传递编码的 URI,则会导致 EOFError(到达文件结尾)
令人惊奇的是,我在使用 apigee 的 FB API 时可以看到位置 URL,

这里是 Net:HTTP 文档上推荐的重定向方法:
有什么需要修改的吗?或者有没有更简单的方法来做到这一点? 谢谢你!!

def self.fetch(uri_str, limit = 10)
  response = Net::HTTP.get_response(URI.parse(uri_str))
  case response
    when Net::HTTPSuccess     then response
    when Net::HTTPRedirection then fetch(response['location'], limit - 1)
  else
    response.error!
  end
end

Hello
i am trying to get Facebook user's album's cover picture.
as it's said in the API page, it returns "An HTTP 302 with the URL of the album's cover picture" when getting:
http s://graph.facebook.com/[album_id]}/picture?access_token=blahblahblah...
documents here: http://developers.facebook.com/docs/reference/api/album

i've tried HTTParty, Net:HTTP and also the RedirectFollower class
HTTParty returns the picture image itself, and no "location" (URL) information anywhere
NET:HTTP and RedirectFollower are a bit tricky...
if i don't use URI.encode when passing the URL into the get method, it causes "bad uri" error
but if i use URI.encode to pass the encoded URI, it causes EOFError (end of file reached)
what's amazing is that i can see the location URL when using apigee's FB API

here is the redirect method which is recommended on the Net:HTTP documents:
anything should be modified? or is there any easier way to do this?
thank you!!

def self.fetch(uri_str, limit = 10)
  response = Net::HTTP.get_response(URI.parse(uri_str))
  case response
    when Net::HTTPSuccess     then response
    when Net::HTTPRedirection then fetch(response['location'], limit - 1)
  else
    response.error!
  end
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迟到的我 2024-10-13 11:12:54

如果您不介意使用 gem,curb 将为您完成此操作。这一切都与使用 follow_location 参数有关:

gem 'curb'
require 'curb'

# http://www.stackoverflow.com/ redirects to http://stackoverflow.com/

result = Curl::Easy.http_get("http://www.stackoverflow.com/") do |curl|
 curl.follow_location = true
end

puts result.body_str

但这并不是唯一具有此功能的库。

请注意,很多时候您会在标头中获得无效位置,并且必须由用户代理对其进行解释才能将其呈现为有用的内容。像 Location: / 这样的标头需要重写才能被获取。其他时候,您会收到类似 Location: uri=... 的标头,并且您必须从那里提取位置。最好将其留给您的库,而不是您自己重写。

If you don't mind using a gem, curb will do this for you. It's all about using the follow_location parameter:

gem 'curb'
require 'curb'

# http://www.stackoverflow.com/ redirects to http://stackoverflow.com/

result = Curl::Easy.http_get("http://www.stackoverflow.com/") do |curl|
 curl.follow_location = true
end

puts result.body_str

This is not the only library with this feature, though.

As a note, many times you will get an invalid location in the header and it will have to be interpreted by the user agent to render it into something useful. A header like Location: / will need to be re-written before it can be fetched. Other times you will get a header like Location: uri=... and you'll have to pull out the location from there. It's really best to leave it to your library than re-write that yourself.

偏闹i 2024-10-13 11:12:54

这是我经过一番尝试和错误后得到的结果:

uri_str = URI.encode(https://graph.facebook.com/[album_id]}/picture?access_token=blahblahblah...)

result = Curl::Easy.http_get(uri_str) do |curl|
  curl.follow_location = false
end
puts result.header_str.split('Location: ')[1].split(' ')[0]

返回的 header_str 看起来像
“HTTP blah blah blah 位置:http://xxxxxxx/xxxx.jpg blah blah blah"
所以我设法使用 2 split() 来获取 URL
最终结果是一个干净的 URL
另外,curl.follow_location 应该为 false,这样它就不会返回该页面的正文

here is what i end up with after some trial and error:

uri_str = URI.encode(https://graph.facebook.com/[album_id]}/picture?access_token=blahblahblah...)

result = Curl::Easy.http_get(uri_str) do |curl|
  curl.follow_location = false
end
puts result.header_str.split('Location: ')[1].split(' ')[0]

the returned header_str looks like
"HTTP blah blah blah Location: http://xxxxxxx/xxxx.jpg blah blah blah"
so i managed to get the URL by using 2 split()
the final result is a clean URL
also the curl.follow_location should be false so it won't return the body of that page

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文