Github api 中的基本身份验证与 Ruby 中的 http 请求?

Basic authentication in Github api with http request in Ruby?

我正尝试在 Github api 中使用基本身份验证。我写了这样的东西:

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"

并期望它 returns 用户的回购协议。但是,它不断抛出错误:

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

我知道有一大堆像 githuboctokit 这样的 gem 可以为用户做这些事情。我想自己试试。有谁知道如何在 Ruby 中使用简单的 http 请求进行身份验证?

您需要发出 HTTPS 请求而不是 HTTP。身份验证始终需要使用 SSL。

您还使用了错误的 URL。根据他们的文档 (https://developer.github.com/v3/auth/#basic-authentication),您需要将基本身份验证设置为以下路径:

https://api.github.com/user

并在以下位置提出回购请求:

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

所以你的请求看起来像

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"

我建议查看上面链接的文档,并使用 curl 请求进行首次尝试,因为它们提供了更多信息来帮助您进行调试。