我想在沒有git工具的情況下在GitHub倉庫中創建(推送)新檔案,(因為git工具在我的PHP主機上不可用)。所以我做了一點研究,發現了GitHub REST API。
我嘗試使用curl與個人訪問令牌來上傳或修改我的 repo 中的檔案。這里是一個我找到的外殼代碼,并對curl做了如下修改:
curl -i https://api.github.com/repos/username/repo/newFile.txt -X PUT
-H 'Authorization: token xxxxxxxxxxxxxxxx' -d "{"path": "newFile.txt"。
"訊息": "更新", "內容": "$(openssl base64 -A -in newFile.txt)", "分支":
"master",
"sha": $(curl -X GET https://api.github.com/repos/username/repo/newFile.txt | jq .sha)}"
但是我得到了這個錯誤:
HTTP/1.1 404 Not Found
服務器。GitHub.com
我怎樣才能解決這個問題?
P.S. 即使GitHub或(GitLab)上有一個通過電子郵件推送檔案的服務,這也有助于通過電子郵件在我的repo上創建檔案。
uj5u.com熱心網友回復:
我把我的作業curl放在這里,如果有人需要的話。
為了方便推送,最好把你的檔案做成base64格式(base64 {filename}或在線工具)然后:
curl -X PUT -H "Authorization: token xxxxxxxxxxxxxxxx" https://api.github.com/repos/{owner}/{repo}/contents/{path}/{fileName} -d "{"message": "our commit message", "content": "your file in BASE64"}"
uj5u.com熱心網友回復:
應該是"創建或更新檔案內容" 端點API:
PUT /repos/{owner}/{repo}/contents/{path}。
(我在你的https://api.github.com/repos/username/repo/newFile.txt URL中沒有看到contents)
確保使用最近的PAT(個人訪問令牌),如這里看到的。
還有一個例子:
#! /bin/bash
cartella= " /var/myfolder "
# 更新該檔案
curl -i -X PUT -H ' Authorization: token 4d013330xxxxxxxxxxx ' -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
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/319776.html
標籤:
