V V
V V

Reputation: 822

Ruby github API

I am new to ruby programming. I was trying to write below ruby code to create a comment in Github gist.

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

But I executed below script then I always get {"message"=>"Not Found", "documentation_url"=>"https://developer.github.com/v3"}

Don't know what I am doing wrong. Appreciate help.

Upvotes: 3

Views: 1518

Answers (1)

fny
fny

Reputation: 33635

Make sure that you're authenticated first. From the Github API docs on 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.

You can do this by creating an OAuth2 token and setting it as an HTTP header:

request['Authorization'] = 'token YOUR_OAUTH_TOKEN'

You can also pass the OAuth2 token as a POST parameter:

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'

Upvotes: 4

Related Questions