集成passport-github和node-github

时间:2014-08-03 03:45:48

标签: node.js authentication github passport.js

我正在使用passport-github来允许用户使用Github OAuth在我的应用中进行身份验证。我将访问令牌保存到DB,以便以后可以使用它。

现在我正在尝试使用node-github进行一些API调用,但我无法通过身份验证进行身份验证。用户和密码身份验证不是一个选项,因为我试图避免用户每次都必须进行身份验证,而其他方法(根据自述文件有四种身份验证方法)不起作用(或者至少我无法让他们工作)。

以下是一些相关代码:

exports.create = function( req, res ) {

var project = new Project( req.body );
project.user = req.user;

var token = req.user.providerData.refreshToken.access_token; //Token gotten from passport-github

var GitHubApi = require( 'github' );

var github = new GitHubApi( {
    // required
    version:    '3.0.0',
    // optional
    debug:      true,
    protocol:   'https',
    pathPrefix: '/api/v3', // for some GHEs
    timeout:    5000
} );

github.authenticate( {
    type:  'oauth',
    token: token
} );

github.repos.create(
    {
        headers:     {},
        name:        project.githubRepoUrl,
        description: project.description,
        private:     project.isPrivate,
        auto_init:   true
    },
    function( err, res ) {
        if( err ) {
            console.log( err );
        }

        if( res ) {
            console.log( 'Created repo with id: ' + res.id + 'and name: ' + res.name )
        }
    }
);

相关控制台输出:

REQUEST:  { host: 'api.github.com',
port: 443,
path: '/api/v3/user/repos?access_token=3488bfe013e9c071f9af5a45bda8f8bcd23c7d5a',
method: 'post',
headers: 
   { host: 'api.github.com',
     'content-length': 72,
     'content-type': 'application/json; charset=utf-8',
     'user-agent': 'NodeJS HTTP Client',
     accept: 'application/vnd.github.beta+json' } }
REQUEST BODY: {"name":"sdfdsf","description":"dsfds","private":false,"auto_init":true}

STATUS: 401
HEADERS: {"server":"GitHub.com","date":"Sun, 03 Aug 2014 04:22:04 GMT","content-type":"application/json; charset=utf-8","status":"401 Unauthorized","x-github-media-type":"github.beta; format=json","x-ratelimit-limit":"60","x-ratelimit-remaining":"59","x-ratelimit-reset":"1407043324","x-xss-protection":"1; mode=block","x-frame-options":"deny","content-security-policy":"default-src 'none'","content-length":"83","access-control-allow-credentials":"true","access-control-expose-headers":"ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval","access-control-allow-origin":"*","x-github-request-id":"40E45687:2B9A:596F677:53DDB8EC","strict-transport-security":"max-age=31536000; includeSubdomains","x-content-type-options":"nosniff"}
[error] { [Error: {"message":"Bad credentials","documentation_url":"https://developer.github.com/v3"}]
[error]   message: '{"message":"Bad credentials","documentation_url":"https://developer.github.com/v3"}',
[error]   code: 401 } null undefined
{ [Error: {"message":"Bad credentials","documentation_url":"https://developer.github.com/v3"}]
message: '{"message":"Bad credentials","documentation_url":"https://developer.github.com/v3"}',
code: 401 }

有人之前做过这件事吗?有没有这方面的教程?

0 个答案:

没有答案