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

标签 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(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" 作为请求参数的一部分传递,但没有用。我还尝试在网址中传递用户名

据我所知the docs对此什么也不说。

最佳答案

您必须存储从护照身份验证回调中获得的访问 token 。例如,使用 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);
                    });
                }
            })
        });
    }
));

然后,当您反序列化用户时,您将获得带有访问 token 的用户:

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_token 放入 github API 请求的 Authorization header 中:

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,而不是手动构建请求。为您做这件事的图书馆。您可以找到完整的示例 here Passport-github 与 octonode 和 mongodb

关于node.js - 如何通过 API 发布要点(非匿名),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47244175/

相关文章:

github - 使用 Github GraphQL 获取 secret 要点

mysql - 在连接到真实数据库的 gitlab CI 中运行单元测试

javascript - 带有 Angular 的 socket.io 不会立即显示消息

javascript - React.js 和 webpack 2,需要一个解决方案

git - Jenkins 在 github probject 中访问 msbuild 文件

javascript - 使用 Node GitHub API 在本地克隆远程仓库

github - 如何使用 Github 的 v4 GraphQL API 按编号查询拉取请求?

javascript - 如果我们在任何对象的 Node.js 中扩展原型(prototype)有什么问题?

github - 无法通过 github-pr-coverage-status-plugin 中的 JsonPath : $. metrics.covered_percent 提取浮点值

git - 如何将提交分成多个 squashes