如何解决从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
像这样:
- 转到https://github.com/login/oauth/authorize?client_id=my-client-id
- 登录并授权 通过与
- 在上面的脚本中使用
https://github.com/login/oauth/access_token
。
code
,access_token
,{{1} }作为参数。
有什么我可能想念的吗?还是在使用client_id
而不是client_secret
和code
时出现权限问题?
我的令牌工作正常,因为如果我使用无效的令牌,响应将变为:
access_token
似乎没有足够的权限来使用access_token
与username
和password
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。