1

我正在使用 OmniAuth 通过 Github 对用户进行身份验证。OmniAuth 提供访问令牌。现在我想将 GET 或 POST 请求发送到 Github。我不想使用任何 gem,我想使用 Net::HTTP。我是这样做的:

<%consumer = OAuth::Consumer.new("mshsD0jpgcYwwOEcTW5ZTA",  "V6KTqllY5jS392pj4FNFCb5EiOM8DaFzVwr9cS54XQ", { :site => "https://api.github.com", :request_token_path => '/oauth/request_token', :access_token_path => '/oauth/access_token', :authorize_path => '/oauth/authorize', :scheme => :header })%>

<%access_token = OAuth::AccessToken.new(consumer,auth.token,auth.secret)%>

我以前为 Twitter 做的同样工作很好,但现在我收到以下错误:

uninitialized constant ActionView::CompiledTemplates::OAuth

即使在同一个应用程序中,同样的事情也适用于 Twitter,但不适用于 Github。

我通过谷歌搜索但没有发现任何帮助。

4

2 回答 2

2

You should be using OAuth2 instead of OAuth. I'd actually recommend using Octokit, it's easy to use and Wynn works for GitHub now so part of his job is keeping it up to date. :)

If you want to use Net::HTTP (although I can't imagine why), you can actually do that without any gems. Just put the token you got from OmniAuth in the request's "Authentication" header.

require 'net/https'
require 'uri'

uri = uri = URI.parse("https://api.github.com/users/username")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true

headers = { "Authentication" => "token" }
request = Net::HTTP::Get.new(uri.request_uri, headers)
response = http.request(request)
response.body # => A string containing the JSON response
于 2012-06-05T02:29:07.780 回答
0

鉴于您已经在使用 Omniauth 并且熟悉它,我建议您使用 omniauth-github 策略:https ://github.com/intridea/omniauth-github

于 2012-06-05T04:32:41.690 回答