Ruby github API

I am new to ruby ​​programming. I was trying to write below ruby ​​code to create 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.

+3


source to share


1 answer


Make sure you are authenticated first. From the Github API docs on Authentication :

There are three ways to authenticate through the GitHub API v3. Requests that require authentication will return 404 Not Found instead of 403 Forbidden in some places. This prevents unauthorized users from accidentally leaking private storage.

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'

      

+3


source







All Articles