【发布时间】:2021-12-31 18:53:08
【问题描述】:
我的目标是使用 GitHub Pages 和一个简单的按钮创建一个静态页面。
当我按下此按钮时,我想在用于 GitHub 页面的同一分支中的同一存储库中创建一个文件。
我阅读了一些关于在获取请求中使用授权令牌来创建文件的信息,所以我创建了我的令牌,我将它放入我用来访问存储库的 GitHub 页面的 index.html 中。
很快我收到了一封来自 GitHub 的电子邮件,告诉我这个令牌已被撤销。
政策描述如下:https://github.blog/2015-02-05-keeping-github-oauth-tokens-safe/
我现在的问题是:我怎样才能完成这项任务?
我使用的sn-p如下:
async function createNewFileFunction() {
const user = { name: 'XXX', surname: 'YYY' };
const token = 'ghp_abcdefghijklmnopqrstuvwxyz1234567890';
const api_url = "https://api.github.com/repos/my_account/my_repo/contents/test.txt?ref=gh-pages";
try {
const response = await fetch(api_url, {
method: 'POST',
headers: {
'Authorization': `token ${token}`,
'Content-Type': 'application/json;charset=utf-8'
},
body: JSON.stringify(user)
}
);
console.log(await response.json());
} catch (error) {
console.log('Request Failed', error);
}
}
【问题讨论】:
标签: javascript authorization http-post access-token github-api