[英]Create gist on github with User ID using Node
我正在嘗試使用用戶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范圍是必需的。” 這將為我提供用戶令牌。 但是我如何指定我必須使用用戶X創建要點。我正在閱讀node-githu 文檔,但沒有告訴我。 任何想法?
更新后,我可以根據文檔創建程序化令牌。 如果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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.