Ruby github API

Ruby 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"}

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

确保您先通过身份验证。来自 Github API 文档 Authentication:

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 令牌作为 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'