我正在学习NodeJS,并一直在尝试使用请求模块来玩GithubAPI。但是我不能收到看跌请求-给回购加注星标-可以工作。
这是我的代码:
let repo = JSON.parse(response);
client.get(req.cookies.sessionid, function (err, response) {
request.put("https://api.github.com/user/starred/" + repo.author + "/" + repo.name, {
headers:{
'User-Agent': 'request',
'Authorization':"token "+response,
"Content-Length":0,
}
}, function (errors, response, body) {
console.log(errors);
})
}
在我看来,我遵循了API instructions,但这是我得到的响应:{"message":"Not Found","documentation_url":"https://developer.github.com/v3/activity/starring/#star-a-repository"}
。但是,回购所有者和名称正确。我想念什么?
编辑
我也尝试过使用this软件包,但得到了相同的错误消息。我还尝试过更改身份验证令牌,并收到401错误的身份验证错误,并且以前已经修复了标头错误。我也仔细检查了URL和回购所有者/名称是否正确。
答案 0 :(得分:0)
This isn't NodeJS specific, but there must be something wrong with your request. This JS ajax method sets the request type as PUT which is used for replacing resources or collections.
As the documentation specifies, a 'starred' request requires no body attribute so the Content-Length header must be set to zero.
var addToFavorites = function(repoObj){
$.ajax({
url: 'https://api.github.com/user/starred/<the author>/<the repo>',
type: 'PUT',
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "token " + authStr);
}
}).done(function(response) {
console.log(response);
}).error(function(err) {
console.log(err);
});
};
As for your authentication token, it needs the 'public_repo' scope to utilize starred features, so make sure you define and create your personal access token correctly ahead of time.