3

我想在没有git工具的情况下在 GitHub 存储库上创建(推送)新文件,(因为该工具在我的主机git上不可用)。PHP所以我做了一些研究,发现了GitHub REST API

我尝试使用curl个人访问令牌来上传或修改我的仓库中的文件。这是curl我找到的一个 shell 代码,并进行了如下更改:

curl -i https://api.github.com/repos/username/repo/newFile.txt -X PUT 
\ -H 'Authorization: token xxxxxxxxxxxxxxxxxxxx' -d "{\"path\": \"newFile.txt\", 
\ \"message\": \"update\", \"content\": \"$(openssl base64 -A -in newFile.txt)\", \"branch\": 
\"master\",
\ \"sha\": $(curl -X GET https://api.github.com/repos/username/repo/newFile.txt  | jq .sha)}" 

但我收到此错误:

HTTP/1.1 404 未找到

服务器:GitHub.com

我该如何解决这个问题?

PS 即使push file by emailGitHub 或 (GitLab) 上有服务,这也可以帮助通过电子邮件在我的 repo 上创建文件。

4

2 回答 2

3

我把我的工作放在curl这里,如果有人需要的话。

为了方便推送,最好将您的文件设为 base64格式(base64 {filename}在线工具),然后:

curl -X PUT -H "Authorization: token xxxxxxxxxxxxxxxxxxxx" https://api.github.com/repos/{owner}/{repo}/contents/{path}/{fileName} -d "{\"message\":\"your commit message\",\"content\":\"your file in BASE64\"}"
于 2021-09-06T09:11:59.070 回答
2

它应该是“创建或更新文件内容”端点 API:

PUT /repos/{owner}/{repo}/contents/{path}

contents(我在您的https://api.github.com/repos/username/repo/newFile.txt网址中没有看到)

确保使用最近的 PAT(个人访问令牌),如此处所示
还有一个例子

#! /bin/bash
 
cartella= " /var/myfolder "
 
# update the file
curl -i -X ​​PUT -H ' Authorization: token 4d013330xxxxxxxxxxxxxxx ' -d " { \" path \" : \" mattei.csv \" , \
\" message \" : \" update \" , \" content \" : \" $( openssl base64 -A -in $cartella /mattei.csv ) \" , \" branch \" : \" master \" , \
\" sha \" : $( curl -X GET https://api.github.com/repos/username/repo/contents/mattei.csv | jq .sha ) } " \
https://api.github.com/repos/username/repo/contents/mattei.csv
于 2021-09-06T06:33:40.860 回答