Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何通过API发布要点(非匿名)_Node.js_Github_Github Api_Axios_Gist - Fatal编程技术网

Node.js 如何通过API发布要点(非匿名)

Node.js 如何通过API发布要点(非匿名),node.js,github,github-api,axios,gist,Node.js,Github,Github Api,Axios,Gist,我正在使用passport-github策略 passport.use(new Strategy({ clientID: "...", clientSecret: "...", callbackURL: 'http://localhost:3000/login/github/return', scope: 'gist' }, function(accessToken, refreshToken, profile, cb) { return cb(n

我正在使用
passport-github
策略

passport.use(new Strategy({
    clientID: "...",
    clientSecret: "...",
    callbackURL: 'http://localhost:3000/login/github/return',
    scope: 'gist'
  },
  function(accessToken, refreshToken, profile, cb) {
    return cb(null, profile);
  }));
然后我发出
POST
请求

app.get('/profile/post',
  require('connect-ensure-login').ensureLoggedIn(),
  function(req, res){
var url = 'https://api.github.com/gists';

axios.post(url, {
  method: "POST",
  "description": "POSTING FROM EXPRESS",
  "public": true,
  "files": {
    "file1.txt": {
      "content": "EXPRESS "
    }

}
})
  .then(function (response) {...})
  .catch(function (error) { ... });
要点是匿名创建的

我尝试将
“owner”
“user”
作为请求参数的一部分传递,但没有用。我还尝试在url中传递用户名


据我所知,我对此一无所知。

您必须存储从passport身份验证回调中获得的访问令牌。例如,使用mongo和mongoose存储用户:

passport.use(new GitHubStrategy({
        clientID: "...",
        clientSecret: "...",
        callbackURL: 'http://localhost:3000/login/github/return'
    },
    function(accessToken, refreshToken, profile, done) {

        process.nextTick(function() {

            User.findOne({ id: profile.id }, function(err, res) {
                if (err)
                    return done(err);
                if (res) {
                    console.log("user exists");
                    return done(null, res);
                } else {
                    console.log("insert user");
                    var user = new User({
                        id: profile.id,
                        access_token: accessToken,
                        refresh_token: refreshToken
                    });
                    user.save(function(err) {
                        if (err)
                            return done(err);
                        return done(null, user);
                    });
                }
            })
        });
    }
));
然后,当您反序列化该用户时,您将使用访问令牌返回该用户:

passport.serializeUser(function(user, done) {
    done(null, user.id);
});

passport.deserializeUser(function(id, done) {
    User.findOne({ "id": id }, function(err, user) {
        done(err, user);
    });
});
现在在您的端点中,将
req.user.access\u令牌放入github API请求的
Authorization
头中:

app.get('/profile/post', 
  require('connect-ensure-login').ensureLoggedIn(),
  function(req, res) {

    axios.post('https://api.github.com/gists', {
      "method": "POST",
      "description": "POSTING FROM EXPRESS",
      "headers": {
        "Authorization" : "token " + req.user.access_token
      },
      "public": true,
      "files": {
        "file1.txt": {
          "content": "EXPRESS "
        }
      }
    })
    .then(function (response) {...})
    .catch(function (error) { ... });
  }
);
但是,您可以使用为您执行此操作的库,而不是手动生成请求。您可以找到带有octonode和mongodb的passport github的完整示例