为了账号安全,请及时绑定邮箱和手机立即绑定

想请教个问题:为什么我在使用 rugged 进行 git push 操作时 报了 401 错误?

想请教个问题:为什么我在使用 rugged 进行 git push 操作时 报了 401 错误?

Go
摇曳的蔷薇 2023-03-31 13:13:38
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/rugged-0.24.0/lib/rugged/repository.rb:224:in `push': Request failed with status code: 401 (Rugged::NetworkError)        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/rugged-0.24.0/lib/rugged/repository.rb:224:in `push'        from mytest.rb:34:in `<main>' 下面是我的代码 require 'rugged'git_email = '138XXXX@139.com'git_name = 'XXXX'repo_name = 'D://mytestcode//rubyOntest'repo = Rugged::Repository.new('D://mytestcode//rubyOntest')puts "1"index = repo.indexputs "3"oid = repo.write("This is a blob.", :blob)#index.add(:path => "readme.txt", :oid => oid, :mode => 0100644)index.add_allputs "4"options = {}options[:tree] = index.write_tree(repo)puts "5"options[:author] = { :email => git_email, :name => git_name, :time => Time.now }options[:committer] = { :email => git_email, :name => git_name, :time => Time.now }puts "6"options[:message] ||= "Making a commit via Rugged!--add  all"options[:parents] = repo.empty? ? [] : [ repo.head.target ].compactoptions[:update_ref] = 'HEAD'puts "7"objuser=Rugged::Credentials::UserPassword.new(options)puts "8"Rugged::Commit.create(repo, options)puts "9"repo.push 'origin'# this is my errorputs "Done"
查看完整描述

2 回答

?
波斯汪

TA贡献1811条经验 获得超4个赞

401代表http未授权,你的repo.push方法调用未带授权信息 

repo.push/remote.push方法原型


查看完整回答
反对 回复 2023-04-03
?
Helenr

TA贡献1780条经验 获得超3个赞

objuser=Rugged::Credentials::UserPassword.new({username: '用户名', password: '密码'})
puts "add credentials"
Rugged::Commit.create(repo, options)
puts "Commit ..."
remote = repo.remotes["origin"]
puts remote.url
#remote = Rugged::Remote.lookup(@repo , 'origin') 

#使用credentials进行授权即可。
remote.push("refs/heads/master", {credentials: objuser})


查看完整回答
反对 回复 2023-04-03
  • 2 回答
  • 0 关注
  • 69 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信