Curl 如何在Google Apps脚本中使用UrlFetchApp更新Github repo?

Curl 如何在Google Apps脚本中使用UrlFetchApp更新Github repo?,curl,google-apps-script,http-headers,github-api,Curl,Google Apps Script,Http Headers,Github Api,问候-我正在尝试使用Github API更新我在Google应用程序脚本中生成的文件。我正在Github页面上显示文件(图像)。不幸的是,我对GithubAPI和HTTP请求的了解很差 如何配置UrlFetchApp将文件提交到我的Github repo?我认为类似的想法也表达在中,但我不知道如何将其准确地转换为应用程序脚本,我发现很难针对API进行调试。非常感谢你的帮助 发件人: 如果要将此线程的以下curl命令转换为Google Apps脚本 curl -i -X PUT \ -H 'A

问候-我正在尝试使用Github API更新我在Google应用程序脚本中生成的文件。我正在Github页面上显示文件(图像)。不幸的是,我对GithubAPI和HTTP请求的了解很差

如何配置
UrlFetchApp
将文件提交到我的Github repo?我认为类似的想法也表达在中,但我不知道如何将其准确地转换为应用程序脚本,我发现很难针对API进行调试。非常感谢你的帮助

发件人: 如果要将此线程的以下curl命令转换为Google Apps脚本

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>
curl-i-X PUT\
-H'授权:令牌'\
-d“{”路径“:”消息“:”提交者“{”姓名“:”电子邮件“:”}”,内容“:”分支“:”主“}”\
https://api.github.com/repos///contents/
致: 它变成如下

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())
}
函数myFunction(){
const fileId=“####”;//请在此处设置要上载的文件ID。
const content=Utilities.base64Encode(DriveApp.getFileById(fileId.getBlob().getBytes());
常量url=”https://api.github.com/repos///contents/";
const data={“路径”:“消息”:“提交者”:{“名称”:“电子邮件”:“},“内容”:内容,“分支”:“主”};
常量参数={
方法:“放”,
有效载荷:JSON.stringify(数据),
标题:{
授权:`token`,
//Accept:“application/vnd.github.v3+json”//即使不使用此选项,请求也可以工作。
}
};
const res=UrlFetchApp.fetch(url,参数);
console.log(res.getContentText())
}
  • 关于
    ,例如,当您想将
    sample.txt
    放入
    /sample
    目录下的时,请使用类似
    https://api.github.com/repos///contents/sample/sample.txt
注:
  • 在这种情况下,您的令牌需要能够被使用。所以请小心这个
参考资料: