我是 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/