如何在Rails中获取302重定向位置? (已尝试过 HTTParty、Net/Http 和 RedirectFollower)
你好
我正在尝试获取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您不介意使用 gem,
curb
将为您完成此操作。这一切都与使用follow_location
参数有关:但这并不是唯一具有此功能的库。
请注意,很多时候您会在标头中获得无效位置,并且必须由用户代理对其进行解释才能将其呈现为有用的内容。像
Location: /
这样的标头需要重写才能被获取。其他时候,您会收到类似Location: uri=...
的标头,并且您必须从那里提取位置。最好将其留给您的库,而不是您自己重写。If you don't mind using a gem,
curb
will do this for you. It's all about using thefollow_location
parameter: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 likeLocation: 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.这是我经过一番尝试和错误后得到的结果:
返回的 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:
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