Die*_*ego 5 github node.js express
我有使用 Express 和 node-github 的节点示例。我正在请求令牌,以便用户可以授权应用程序创建要点。我遇到的问题是,要点是像匿名用户一样创建的。
如果我从代码要点中删除 github.authenticate ,则会以匿名方式创建。如果我离开 github.authenticate ,则不会创建任何要点,也不会显示任何错误。
我认为这个问题在于我必须在哪里找到 github.authenticate。
我有我的回调
app.get('/auth/github/callback',function (req, res) {
var url = Url.parse(req.url);
var path = url.pathname;
var query = querystring.parse(url.query);
var code = req.query.code;
console.log('/callback');
OAuth2.AuthCode.getToken({
code: code,
redirect_uri: 'http://127.0.0.1:3000/auth/github/callback'
}, saveToken);
github.authenticate({
type: "oauth",
token: accessToken
});
res.redirect('home');
function saveToken(error, result) {
if (error) { console.log('Access Token Error', error.message); }
accessToken = OAuth2.AccessToken.create(result);
}
});
Run Code Online (Sandbox Code Playgroud)
这是我的帖子。到这里,要点就创建好了。
app.post('/test', function(req, res){
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></html>"
}
}
}, function(err, rest) {
console.log(rest);
res.render('/');
});
});
Run Code Online (Sandbox Code Playgroud)
我一直在尝试寻找类似的问题,但只是找到了这个问题以及在哪里使用我正在使用的模块的答案。
我找到了解决方案。我没有使用简单的oauth,而是将其更改为oauth。希望它能帮助某人。
var oauth = require("oauth").OAuth2;
var OAuth2 = new oauth(GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET, "https://github.com/", "login/oauth/authorize", "login/oauth/access_token");
app.get('/auth/github',function(req,res){
res.writeHead(303, {
Location: OAuth2.getAuthorizeUrl({
redirect_uri: 'http://127.0.0.1:3000/auth/github/callback',
scope: "user,repo,gist"
})
});
res.end();
});
app.get('/auth/github/callback',function (req, res) {
var code = req.query.code;
OAuth2.getOAuthAccessToken(code, {}, function (err, access_token, refresh_token) {
if (err) {
console.log(err);
}
accessToken = access_token;
// authenticate github API
console.log("AccessToken: "+accessToken+"\n");
github.authenticate({
type: "oauth",
token: accessToken
});
});
res.redirect('home');
});
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></html>"
}
}
}, function(err, rest) {
console.log(rest);
console.log(err);
res.render('/');
});
Run Code Online (Sandbox Code Playgroud)