ruby-on-rails - ruby github API

标签 ruby-on-rails ruby github github-api

我是 ruby​​ 编程的新手。我试图在下面编写 ruby​​ 代码以在 Github 要点中创建评论。

uri = URI.parse("https://api.github.com/gists/xxxxxxxxxxx/comments")

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = (uri.scheme == 'https')
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(uri.request_uri)
request.body = {
          "body" => "Chef run failed "
        }.to_json
        response = http.request(request)
        response2 = JSON.parse(response.body)
puts response2

但是我执行了下面的脚本然后我总是得到 {"message"=>"Not Found", "documentation_url"=>"https://developer.github.com/v3"}

不知道我做错了什么。感谢帮助。

最佳答案

确保您首先经过身份验证。来自 Authentication 上的 Github API 文档:

There are three ways to authenticate through GitHub API v3. Requests that require authentication will return 404 Not Found, instead of 403 Forbidden, in some places. This is to prevent the accidental leakage of private repositories to unauthorized users.

您可以通过创建一个 OAuth2 token 来做到这一点并将其设置为 HTTP header :

request['Authorization'] = 'token YOUR_OAUTH_TOKEN'

您还可以将 OAuth2 token 作为 POST 参数传递:

uri.query = URI.encode_www_form(access_token: 'YOUR_OAUTH_TOKEN')
# Encoding really isn't necessary though for this though, so this should suffice
uri.query = 'access_token=YOUR_OAUTH_TOKEN'

关于ruby-on-rails - ruby github API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30388516/

相关文章:

ruby-on-rails - 在 Controller 规范 (rspec) 中测试设计和 cancan

eclipse - Egit ssh 问题

ruby - Elixir:String Force encode UTF-8

ruby - 以 find 开头的枚举器链

opencv - Tesseract&Opencv程序镜像问题显示一些错误

git - 重置 --hard git 远程分支到远程 reflog 中的 SHA 但不在本地 repo 中

ruby-on-rails - 使用 LDAP 和本地数据库进行 Rails 身份验证

css - 每个操作的 Rails scss 样式表

ruby-on-rails - ActiveRecord 事务中的错误处理?

Ruby 应用程序和 Resque : can't start workers