我正在尝试使用用户ID在github.com上创建要点。目前,我可以使用node-github模块创建匿名要点。
这是代码

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);
  });


根据github文档,“要代表用户读取或写入要点,则要点OAuth范围是必需的。”这将为我提供用户令牌。
但是,如何指定我要使用用户X创建要点。我正在读取node-githu documentation,但没有告诉我。任何想法?

更新
我可以根据文档创建程序化令牌。如果create方法未引用要创建的要点,仍然不确定如何识别用户的ID。

github.authorization.create({
    scopes: ["user", "public_repo", "repo", "repo:status", "gist"],
    note: "what this auth is for",
    note_url: "http://url-to-this-auth-app",
    headers: {
        "X-GitHub-OTP": "two-factor-code"
    }
}, function(err, res) {
    if (res.token) {
        //save and use res.token as in the Oauth process above from now on
    }
});

最佳答案

您与我分享的链接之一就是答案。只需对其进行修改。
如果有人需要,这里是代码。

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);

关于javascript - 使用Node在GitHub上使用用户ID创建要点,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24177783/

10-12 15:12