【发布时间】:2014-02-03 00:57:31
【问题描述】:
我正在尝试使用 libgit2 获取私有 github 存储库。回购属于一个组织,我有读/写权限。该程序是用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/transsports/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 身份验证失败了吗?有没有办法避免每次操作都必须输入用户名/密码(使用明文身份验证)?
【问题讨论】:
-
你一直提到“GITHUBUERNAME”,但是对于 SSH 身份验证,ssh 用户是“git”。关键是用来确定 GitHub 用户名是什么。
-
好吧,我太傻了。是的,用户是 git。有了这个,它就可以工作了。如果你想给出一个答案,我会认为它是有效的。谢谢
-
@Tonidero 那么这里的用户名是什么?
标签: macos authentication github ssh libgit2