gpt4 book ai didi

javascript - 使用 Node 使用用户 ID 在 github 上创建要点

转载 作者:太空宇宙 更新时间:2023-11-04 01:04:45 25 4
gpt4 key购买 nike

我正在尝试使用用户 ID 在 github.com 上创建要点。目前我可以使用 node-github 模块创建匿名要点。这是代码

github.gists.create({
"description": "the description for this gist",
"public": true,
"files": {
"BONE101TEST_2.md": {
"content": "<html><h1>This is a Test!</h1><b>Hello</b><img src=></html>"
}
}
}, function(err, rest) {
console.log(rest);
});

根据github文档“代表用户读取或写入要点,需要要点 OAuth 范围。”这将为我提供用户的 token 。但是我如何指定我希望必须使用用户 X 创建要点。我正在阅读 node-githu documentation但没有告诉我这一点。有什么想法吗?

已更新我可以根据文档创建一个程序 token 。如果 create 方法没有引用它,仍然不确定如何识别用于创建要点的用户 id。

github.authorization.create({
scopes: ["user", "public_repo", "repo", "repo:status", "gist"],
note: "what this auth is for",
note_url: "http://url-to-this-auth-app",
headers: {
"X-GitHub-OTP": "two-factor-code"
}
}, function(err, res) {
if (res.token) {
//save and use res.token as in the Oauth process above from now on
}
});

最佳答案

在您与我分享的链接之一中找到了答案。修改一下就可以了。如果有人需要的话,这是代码。

http.createServer(function(req, res) {
var url = Url.parse(req.url);
var path = url.pathname;
var query = querystring.parse(url.query);

if (path == "/" || path.match(/^\/user\/?$/)) {
// redirect to github if there is no access token
if (!accessToken) {
res.writeHead(303, {
Location: oauth.getAuthorizeUrl({
redirect_uri: 'http://localhost:3000/github-callback',
scope: "user,repo,gist"
})
});
res.end();
return;
}

// use github API
github.gists.create({
"description": "the description for this gist",
"public": true,
"files": {
"TEST_2.md": {
"content": "<html><h1>This is a Test!</h1><b>Hello</b><img src=></html>"
}
}
}, function(err, rest) {
console.log(rest);
});
return;
}
// URL called by github after authenticating
else if (path.match(/^\/github-callback\/?$/)) {
// upgrade the code to an access token
oauth.getOAuthAccessToken(query.code, {}, function (err, access_token, refresh_token) {
if (err) {
console.log(err);
res.writeHead(500);
res.end(err + "");
return;
}

accessToken = access_token;

// authenticate github API
github.authenticate({
type: "oauth",
token: accessToken
});

//redirect back
res.writeHead(303, {
Location: "/"
});
res.end();
});
return;
}

res.writeHead(404);
res.end("404 - Not found");
}).listen(3000);

关于javascript - 使用 Node 使用用户 ID 在 github 上创建要点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24177783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com