【问题标题】:Rails - Sending a GET RequestRails - 发送 GET 请求
【发布时间】:2016-04-18 19:43:35
【问题描述】:

我正在尝试在提交表单时检索用户的 Google 帐户信息。为此,我必须向 url 发出 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>

谢谢!

【问题讨论】:

  • 你如何向 google 发出 GET 请求?
  • 是的,这是我的问题。
  • 你没有提供你的代码来分析
  • 这不是一个真正的代码特定问题,因为它是如何发出 GET 请求的。
  • 直接获取/发布调用,例如excongem

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


【解决方案1】:

你可以使用excon gem 向外部资源发出特定的CRUD 或仅get 请求,例如应该是这样的:

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

PS:但您似乎不需要直接 gem,而是使用 google-api gem,正如 documentation 中所述。

【讨论】:

  • 我会通过Excon.get('http://geemus.com', :headers => {'Authorization' => 'Basic 0123456789ABCDEF'})提供授权令牌?
  • @Jill 不,您只能通过 oauth google 身份验证获得身份验证令牌
  • 是的,我有令牌,但是如何按照说明中的说明为请求提供令牌。
  • 那个谷歌链接有点混乱。我可以更理解 excon。
【解决方案2】:

我发现 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"})

响应对象将具有“状态代码”、“标题”、“正文”等(无论 youtube 在您的情况下发回什么)。您可以使用

访问其内容
response.parsed_response

get 请求也一样

请在开始之前阅读更多相关信息以便更好地理解。 来源:Documentation & Tutorial

【讨论】:

    猜你喜欢
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-10
    • 1970-01-01
    • 2020-01-24
    相关资源
    最近更新 更多