【问题标题】:How to star github repo with Github API - What's wrong with my request?如何使用 Github API 为 github 存储库加注星标 - 我的请求有什么问题?
【发布时间】:2018-12-07 16:40:15
【问题描述】:

我正在学习 NodeJS,并且一直在尝试使用请求模块来玩 GithubAPI。但是,我无法收到 put 请求 - 为 repo 加注星标 - 工作。

这是我的代码:

        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 和 repo 所有者/名称是否正确。

【问题讨论】:

  • 您确定在您的标头令牌中使用的是完整的响应而不是其中的一个字段吗?还是一个回购领域?这看起来很可疑
  • 每个变量都有它的期望值,我也用硬编码的值试过这个。
  • 你解决了吗?我遇到了同样的问题..
  • 不抱歉从未成功。如果您找到解决方案,我会很高兴阅读它。祝你好运!

标签: node.js github-api node-request


【解决方案1】:

这不是 NodeJS 特定的,但您的请求一定有问题。此 JS ajax 方法将请求类型设置为 PUT,用于替换资源或集合。

如文档所述,“加星标”请求不需要正文属性,因此 Content-Length 标头必须设置为零。

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

至于您的身份验证令牌,它需要 'public_repo' 范围才能利用已加星标的功能,因此请确保提前正确定义和创建您的个人访问令牌。

【讨论】:

    猜你喜欢
    • 2012-12-13
    • 2021-12-16
    • 2020-12-10
    • 2019-02-26
    • 2017-09-01
    • 2019-08-28
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多