rugged 进行git push 操作时 报了401错误
@zouqilin 你好,想跟你请教个问题:我在使用rugged 进行git push 操作时 报了401错误。
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/rugged-0.24.0/lib/rugged/repository.rb:22
4: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/rep
ository.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.index
puts "3"
oid = repo.write("This is a blob.", :blob)
#index.add(:path => "readme.txt", :oid => oid, :mode => 0100644)
index.add_all
puts "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 ].compact
options[: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 error
puts "Done"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
good
问题解决了。
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})
qi lin hao diao , na ge bug hai mei gao ding a ?
谢谢,麻烦能问下如何进行授权么? 是在github 进行ssh 认证么? https://github.com/settings/keys
401代表http未授权,你的repo.push方法调用未带授权信息
repo.push/remote.push方法原型如下链接
http://git.oschina.net/zouqilin/github_rugged/blob/master/ext/rugged/rugged_remote.c#L643
我使用 git bash 是可以进行push的。
你的git现在能推送吗,为什么我一push就会显示Timed out.