1

我有一个简单的 ruby​​ 脚本,它使用他们的 API 从 GitHub 获取数据。

uri = URI.parse("https://api.github.com/search/issues?q=is:pr+is:merged+base:master+repo:organization/respository")
request = Net::HTTP::Get.new(uri)
request.basic_auth('github_username', 'github_password')
request['Accept'] = 'application/json'
request['Content-Type'] = 'application/json'
req_options = {use_ssl: uri.scheme == 'https'}
response =
  Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request request
  end

raise response.body.inspect

上面的请求工作得很好,但是 GitHub 不推荐使用密码身份验证https://developer.github.com/changes/2020-02-14-deprecating-password-auth/),根据他们的指南,我可以使用Web 应用程序流程相反(https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps/#web-application-flow)。

我必须根据他们的指南进行的更改是更改:

curl -u my_user:my_password https://api.github.com/user/repos

curl -H 'Authorization: token my-oauth-token' https://api.github.com/user/repos

在我上面的 ruby​​ 脚本中,我替换了:

request.basic_auth('github_username', 'github_password')

request['Authorization'] = 'token my-access-token'

它没有向我返回数据,而是给了我以下响应:

{"message"=>"Validation Failed", "errors"=>[{"message"=>"The listed users and repositories cannot be searched either because the resources do not exist or you do not have permission to view them.", "resource"=>"Search", "field"=>"q", "code"=>"invalid"}], "documentation_url"=>"https://docs.github.com/v3/search/"}

我是access_token这样的:

有什么我可能错过的吗?或者使用access_token代替usernameand时的权限问题password

我的令牌工作得很好,因为如果我使用无效令牌,响应将变为:

{"message"=>"Bad credentials", "documentation_url"=>"https://docs.github.com/rest"}

似乎没有足够的权限使用access_tokenvsusernamepassword

4

0 回答 0