我正在尝试使用libgit2获取私有github存储库.repo属于一个组织,我有读/写权限.该程序是用Objective-C++编写的.我有这个:
- (int)fetchBranches { git_remote *remote = NULL; int result = 0; if (!(result = git_remote_load(&remote, repo, "origin"))) { git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT; callbacks.credentials = cred_acquire_cb; git_remote_set_callbacks(remote, &callbacks); result = git_remote_fetch(remote); } return result; } int cred_acquire_cb(git_cred **out, const char * url, const char * username_from_url, unsigned int allowed_types, void * payload) { return git_cred_ssh_key_new(out, "GITHUBUSERNAME", "/Users/admin/.ssh/id_rsa.pub", "/Users/admin/.ssh/id_rsa", "PASSPHRASE"); }
这会创建凭据,但是当我执行提取时,尝试使用libssh2对会话进行身份验证时会失败.(libgit2/SRC /运输/ ssh.c:302):
rc = libssh2_userauth_publickey_fromfile( session, c->username, c->publickey, c->privatekey, c->passphrase);
错误是-18,LIBSSH2_ERROR_AUTHENTICATION_FAILED.我已经确认公钥已添加到Github并尝试将其更改为新的公钥.密码也是正确的,用户名是我的github用户名.我也可以通过控制台进行提取.远程配置是:
[remote "origin"] url = git@github.com:ORGNAME/REPONAME.git fetch = +refs/heads/*:refs/remotes/origin/*
我也尝试从OSX中的git-agent获取密钥,结果相同:
return git_cred_ssh_key_from_agent(out, "GITHUBUSERNAME");
最后我尝试使用明文身份验证:
return git_cred_userpass_plaintext_new(out, username, password);
将repo url更改为HTTPS地址后,最后一种方法可行.问题是它需要用户每次输入密码或将包含密码的git_cred结构存储为空白(不喜欢).
任何人都知道为什么ssh身份验证失败?有没有办法避免每次操作都输入用户名/密码(使用明文身份验证)?
通过ssh交谈的用户是"git"(因此所有网址都是git@github.com:user/repo
),所以你应该使用它而不是GitHub上的"真实"用户名.您发送的密钥是服务器用于确定它正在处理哪个用户的密钥.