From:
If you want to convert the following curl command of this thread to Google Apps Script,
curl -i -X PUT \
-H 'Authorization: token <token_string>' \
-d '{"path": "<filename.extension>", "message": "<Commit Message>", "committer": {"name": "<Name>", "email": "<E-Mail>"}, "content": "<Base64 Encoded>", "branch": "master"}' \
https://api.github.com/repos/<owner>/<repository>/contents/<filename.extension>
To:
it becomes as follows.
function myFunction() {
const fileId = "###"; // Please set the file ID you want to upload here.
const content = Utilities.base64Encode(DriveApp.getFileById(fileId).getBlob().getBytes());
const url = "https://api.github.com/repos/<owner>/<repository>/contents/<filename.extension>";
const data = {"path": "<filename.extension>", "message": "<Commit Message>", "committer": {"name": "<Name>", "email": "<E-Mail>"}, "content": content, "branch": "master"};
const params = {
method: "put",
payload: JSON.stringify(data),
headers: {
authorization: `token <token_string>`,
// Accept: "application/vnd.github.v3+json" // Even when this is not used, the request works.
}
};
const res = UrlFetchApp.fetch(url, params);
console.log(res.getContentText())
}
- About
<filename.extension>
, for example, when you want to put sample.txt
to the under the directory of ./sample
, please use the URL like https://api.github.com/repos/<owner>/<repository>/contents/sample/sample.txt
.
Note:
- In this case, your token is required to be able to be used. So please be careful this.
References: