[github] 两步通过curl调用github API远程建立仓库(repository)

win10环境下测试成功的通过Curl调用github API远程建立仓库的办法如下:

  1. github中申请私人tocken(用户名+密码方式测试没能成功)
  2. 本地环境下输入
curl -u "【用户名】:【申请到的tocken】" https://api.github.com/user/repos -d "{\"name\": \"【仓库名称】\"}"

正文:

有通过bash环境调用github API,建立代码库的需求的道友们可以尝试通过上述步骤调用github API接口,建立代码库。
关于这个需求网上的教程不少,也有官方的说明文档,但是各种尝试都没有成功,主要问题是两类:
  • 授权不成功(Requires Authentication)
{
  "message": "Requires authentication",
  "documentation_url": "https://docs.github.com/rest/reference/repos#create-a-repository-for-the-authenticated-user"
}

这个是尝试通过用户名+密码方式获得授权,多次尝试没能成功,后确定授权方式需采用用户名+tocken方式,申请私人tocken方式如下:
在这里插入图片描述
申请时可根据自己的需求选取tocken权限,在使用时以代替账户密码的方式就行。

  • JSON语法不正确(Problems parsing JSON)
{
  "message": "Problems parsing JSON",
  "documentation_url": "https://docs.github.com/rest/reference/repos#create-a-repository-for-the-authenticated-user"
}

这个问题卡了一段时间,原因在于解决方法跟官方document有些不一致(个人理解),官方DOC里给的示例是:

curl \
  -X PATCH \
  -H "Accept: application/vnd.github.v3+json" \
  https://api.github.com/repos/OWNER/REPO \
  -d '{"name":"Hello-World","description":"This is your first repository","homepage":"https://github.com","private":true,"has_issues":true,"has_projects":true,"has_wiki":true}'

Reference here: GitHub Docs

但在实际操作时,需要对花括号({})中的双引号前补充反斜杠(\),否则会提示JSON语法问题。

调整后可成功调用API并建立仓库。

其他:

  • 通过api删除仓库
curl -X DELETE -u  "【用户名】:【申请到的tocken】"  https://api.github.com/repos/【用户名】/【仓库名称】
  • 本地git连接远程仓库
git remote add 【仓库简称】 git@github.com:【用户名】/【仓库名】.git
  • 使用时注意中文中括号(【】)不出现在代码中

Reference:

1. 使用curl操作github API V3(1)
2. curl 的用法指南
3. A curl tutorial using GitHub’s API
3. Problems parsing JSON when creating a new repo

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值