Rails - 发送GET请求

时间:2016-01-13 17:39:05

标签: ruby-on-rails get crud google-signin

我在提交表单时尝试检索用户的Google帐户信息。为了做到这一点,我必须向网址发出GET请求。这是YouTube API文档中的内容

To request the currently logged-in user's profile, send a GET request
to the following URL. Note: For this request, you must provide an
authentication token, which enables YouTube to identify the user.

https://gdata.youtube.com/feeds/api/users/default

https://developers.google.com/youtube/2.0/developers_guide_protocol_profiles?hl=en

如何在提交表单时进行自定义(或特定于此)GET请求,以及如何提供身份验证令牌?可能有用的信息:提交表单后,它将转到VideoController的新方法。

在提出请求后,我该如何访问这些信息?我需要查看给定信息中是否存在<yt:relationship>

谢谢!

2 个答案:

答案 0 :(得分:1)

Yuo可以使用excon gem对外部资源发出特定的CRUDget个请求,例如,它应该是这样的:

Excon.post('http://youtube.com', :body => 'token=sjdlsdjasdljsdlasjda')

PS :但似乎你没有要求使用google-api宝石的直接宝石,如documentation中所述。

答案 1 :(得分:1)

我发现HTTParty gem对于在rails中使用外部API非常有用。

您可以提出请求

 response = HTTParty.post("http://www.example.com", :timeout => 5, :body => "my body content", :query => { hash_of_params_here } ,:headers => {'Content-Type' => 'application/json', 'AuthKey' => "your_key_here"})

响应对象将具有'statuscode','headers','body'等(无论youtube在您的情况下发回)。您可以使用

访问其内容
response.parsed_response

同样适用于获取请求

在开始之前,请仔细阅读有关它的更多信息。 资料来源:Documentation&amp; Tutorial

相关问题