Answers:
github api支持使用用户名和密码进行身份验证:
有三种通过GitHub API v3进行身份验证的方法。...
基本身份验证
$卷曲-U “用户名” https://api.github.com
...
因此,只需以您喜欢的语言选择一个库,然后使用“ 创建公钥 ”“公钥” API部分的实现版本:
创建一个公共密钥。要求您通过至少具有[write:public_key]范围的Basic Auth或OAuth进行身份验证。
输入
POST /user/keys
{
"title": "octocat@octomac",
"key": "ssh-rsa AAA..."
}
如果要从命令行使用它(通过curl):
curl -u "username" --data '{"title":"test-key","key":"ssh-rsa AAA..."}' https://api.github.com/user/keys
甚至不提示输入密码:
curl -u "username:password" --data '{"title":"test-key","key":"ssh-rsa AAA..."}' https://api.github.com/user/keys
这是一个使用curl与github API交互的不错的小教程
与xx4h的答案类似,这就是我在脚本中自动执行新VM设置的方式。
ssh-keygen -t rsa -b 4096 -C "myemailaddress@hotmail.com"
curl -u "myusername" \
--data "{\"title\":\"DevVm_`date +%Y%m%d%H%M%S`\",\"key\":\"`cat ~/.ssh/id_rsa.pub`\"}" \
https://api.github.com/user/keys
它为您提供了一个新的SSH密钥,将其包含在curl调用中,并为GitHub端的每个密钥提供了一个唯一但仍易于识别的名称(例如,现在运行将提供DevVm_150602142247)。
#!/bin/bash
set -xe
myemail="your-email"
#your personal access token
git_api_token="befdf14c152d6f2ad8cff9c5affffffffffffffffff"
#We'll use the HTTPS to push a ssh key to git, SSH for pull/push configuration
gitrepo_ssh="git@github.com:person/repo.git"
gitrepo_https="https://github.com/person/repo.git"
#Generating SSH key:
ssh-keygen -f "${HOME}/.ssh/id_rsa" -t rsa -b 4096 -C "${myemail}" -N ''
sslpub="$(cat ${HOME}/.ssh/id_rsa.pub |tail -1)"
#git API path for posting a new ssh-key:
git_api_addkey="https://api.$(echo ${gitrepo_https} |cut -d'/' -f3)/user/keys"
#lets name the ssh-key in get after the hostname with a timestamp:
git_ssl_keyname="$(hostname)_$(date +%d-%m-%Y)"
#Finally lets post this ssh key:
curl -H "Authorization: token ${git_api_token}" -H "Content-Type: application/json" -X POST -d "{\"title\":\"${git_ssl_keyname}\",\"key\":\"${sslpub}\"}" ${git_api_addkey}
另一个选择是使用API令牌...我在内部gitLab服务器上使用以下内容
片段:
#!/bin/bash
myemail="first.last@domain.com"
# User API token can be found: "https://git.labs.domain.com/profile/account"
git_api_token="m3iP27Jh8KSgNmWAksYp"
# We'll use the HTTPS to push a ssh key to git, SSH for pull/push configuration
gitrepo_ssh="git@git.labs.domain.com:devops/automation.git"
gitrepo_https="https://git.labs.domain.com/devops/automation.git"
########################] D O N O T C H A N G E [########################
# Generating SSH key:
ssh-keygen -f "${HOME}/.ssh/id_rsa" -t rsa -b 4096 -C "${myemail}" -N ''
sslpub="$(cat ${HOME}/.ssh/id_rsa.pub |tail -1)"
# git API path for posting a new ssh-key:
git_api_addkey="https://$(echo ${gitrepo_https} |cut -d'/' -f3)/api/v3/user/keys"
# lets name the ssh-key in get after the hostname with a timestamp:
git_ssl_keyname="$(hostname)-$(date +%Y%m%d%H%M%S)"
# Finally lets post this ssh key:
curl -H "PRIVATE-TOKEN: ${git_api_token}" -H "Content-Type: application/json" \
-X POST -d "{\"title\":\"${git_ssl_keyname}\",\"key\":\"${sslpub}\"}" \
${git_api_addkey}