Adler Hsieh
Adler Hsieh

Reputation: 477

Basic authentication in Github api with http request in Ruby?

I'm trying to use basic authentication in Github api. I wrote something like this:

require 'httpclient'

request = HTTPClient.new
request.set_basic_auth("http://api.github.com/authorizations", "my_username", "my_password")
request.get "http://api.github.com/user/repos"

and expect it returns the repos of a user. However, it keeps throwing an error:

Connection refused - connect(2) for "api.github.com" port 80 (Errno::ECONNREFUSED)

I know there are a whole bunch of gems like github, octokit that do the stuff for users. I want to try it myself. Does anyone know how do I authenticate with a simple http request in Ruby?

Upvotes: 1

Views: 1568

Answers (1)

heading_to_tahiti
heading_to_tahiti

Reputation: 795

You need to make an HTTPS request instead of HTTP. Authentication always needs to use SSL.

You are also using the wrong URL's. As per their docs (https://developer.github.com/v3/auth/#basic-authentication) you need to set your basic authentication to the following path:

https://api.github.com/user

And make your repo request at:

"https://api.github.com/users/<USERNAME>/repos"

So your request would look something like

request = HTTPClient.new
request.set_basic_auth("https://api.github.com/user", "my_username", "my_password")
request.get "https://api.github.com/users/<USERNAME>/repos"

I suggest taking a look at the documentation that I linked to above, and make your first attempt by using curl requests, as they offer more information to help you debug.

Upvotes: 1

Related Questions