从Github basic_auth迁移到Web应用程序流程

问题描述

我有一个简单的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

在上面的红宝石脚本中,我替换了:

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像这样:

有什么我可能想念的吗?还是在使用client_id而不是client_secretcode时出现权限问题?

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

access_token

似乎没有足够的权限来使用access_tokenusernamepassword

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)