Javascript 使用节点在用户ID为的github上创建gist
我正在尝试使用用户ID在github.com上创建gist。目前,我可以使用节点github模块创建匿名gist。 这是密码Javascript 使用节点在用户ID为的github上创建gist,javascript,node.js,github,Javascript,Node.js,Github,我正在尝试使用用户ID在github.com上创建gist。目前,我可以使用节点github模块创建匿名gist。 这是密码 github.gists.create({ "description": "the description for this gist", "public": true, "files": { "BONE101TEST_2.md": { "content": "<html><h1>Th
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);
});
在你与我分享的一个链接中,我找到了答案。只要修改它。 如果有人需要,这是密码
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);
http.createServer(函数(req,res){
var url=url.parse(req.url);
var path=url.pathname;
var query=querystring.parse(url.query);
if(path==“/”| | path.match(/^\/user\/?$/)){
//如果没有访问令牌,则重定向到github
如果(!accessToken){
文书主任(303{
位置:oauth.getAuthorizeUrl({
重定向_uri:'http://localhost:3000/github-回调',
范围:“用户、回购、gist”
})
});
res.end();
返回;
}
//使用GithubAPI
github.gists.create({
“说明”:“本要点的说明”,
“公众”:没错,
“文件”:{
“TEST_2.md”:{
“内容”:“这是一个测试!您好”
}
}
},函数(err,rest){
控制台日志(rest);
});
返回;
}
//github在验证后调用的URL
else if(path.match(/^\/github回调\/?$/){
//将代码升级为访问令牌
getOAuthAccessToken(query.code,{},函数(err,access\u token,refresh\u token){
如果(错误){
控制台日志(err);
书面记录(500);
res.end(err+“”);
返回;
}
accessToken=访问令牌;
//验证github API
github.authenticate({
类型:“oauth”,
令牌:accessToken
});
//重定向回
文书主任(303{
地点:“/”
});
res.end();
});
返回;
}
书面记录(404);
res.end(“404-未找到”);
}).听(3000);
还要检查不起作用的测试程序,我不想请求用户和密码,另一个例子是它指定它不适用于github API的V3://不推荐的Gihub API令牌(似乎不适用于V3 API)
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);