向 Github API 发出 Post 请求以创建问题无效

Making a Post request to Github API for creating issue is not working

过去几天我一直在尝试向 github api 发出此 post 请求,但不幸的是,回复是 "bad message"

这是我们在节点中使用 https 请求在 post 请求中发送的代码片段 -

这是post数据

var issueData = JSON.stringify({
  "title":title,
  "body":comment
});

这是选项文件

var options = {
  host: 'api.github.com',
  path: '/repos/sohilpandya/katasohil/issues?access_token='+sessions.token,
  headers: {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0',
  },
  method: 'POST'
};

这是 https 请求

var requestaddIssue  = https.request(options, function(responseFromIssues){
  responseFromIssues.setEncoding('utf8');
  responseFromIssues.on('data', function(chunk){
    console.log('>>>>chunk>>>>>',chunk);
    issueBody += chunk;
  });
  responseFromIssues.on('end',function(issueBody){
    console.log(issueBody);
  });
});
requestaddIssue.write(issueData);
requestaddIssue.end();

我尝试了另一种方法,其中用户的身份验证令牌在 header 中作为

'Authentication': 'OAuth '+ sessions.token (where we are storing token inside sessions)

但是块响应似乎总是在控制台日志中返回以下内容。

{
 "message": "Not Found",
 "documentation_url": "https://developer.github.com/v3/issues/#create-an-issue"
}

我在 apigee 中尝试过同样的方法,它似乎工作正常并且 returns 可以纠正响应。希望有人能在上面的代码中找到导致此错误消息错误的小错误。

除了您发布的代码段中未定义 issueBody 变量外,代码是正确的。我尝试使用 personal access token.

出现您收到的错误是因为您需要添加一个 scope 来打开问题。

我尝试了 repopublic_repo 范围,它们都在工作。请注意 repo 可以访问私有存储库。在这里你可以看到 list of scopes.

如果您使用的是 OAuth,那么您应该 url 看起来像这样:

https://github.com/login/oauth/authorize?client_id=<client-id>&scope=public_repo&redirect_uri=<redirect-uri>