【问题标题】:GitHub API - Comment on Gist returns 404GitHub API - 评论 Gist 返回 404
【发布时间】:2018-02-15 11:35:21
【问题描述】:

在关注 GitHub API 上的文档后,我一直在提交评论以获取要点,以下代码总是返回 404,并且在 Postman 中也进行了相同的调用。

我的JavaScript代码如下:

const config = {
        method: 'POST',
        headers: {
            'Authorization': credentials.authorizationHeader,
            'Content-Type': 'application/vnd.github.v3.text+json'
        },
        body: { "body": JSON.stringify(comment) } 
    };

    fetch(`https://api.github.com/gists/${gistId}/comments/`, config)
        .then(res => {
            if (res.ok) {
                dispatch(getGistDetails(gistId, credentials));

                dispatch({ type: SUBMIT_COMMENT_SUCCESS });
            } else {
                ToastAndroid.show('An error ocurred, please try again.', ToastAndroid.SHORT);
                console.log(res);
                dispatch({ type: SUBMIT_COMMENT_FAIL });
            }
        })
        .catch(err => console.log(err));

我通过 OAuth 获得的凭据:

accessToken: "redacted"
authorizationHeader:"bearer redacted"
clientID:"redacted"
idToken:null
scopes:"gist"
type:"bearer"

我尝试将authorizationHeader 更改为token <oauth_token,但仍然没有成功。

提前致谢。

【问题讨论】:

  • 404 是Not Found 错误代码。您使用的 url 很可能指向一个不存在的 gist
  • gist 存在。根据 API 的文档,404 通常表示403,但我的凭据(据说)是正确的。
  • 供参考,这是gist我要评论的:https://gist.github.com/desktp/8e376d6cd8d671e14c735d051eff7140
  • 看起来您在上面的查询中有一些隐藏的 UTF 字符...

标签: javascript api github gist


【解决方案1】:

您的 GIST ID 中似乎有一些不可见的非标准字符,我什至无法让您的链接正常工作(或者它是私有的?)

【讨论】:

  • 真是奇怪,我是直接从地址栏复制粘贴的。不过,在我的代码中,要点 id 来自 API。我首先要求提供要点详细信息,这没有问题,但随后评论失败。让我再试试这里的地址:https://gist.github.com/desktp/8e376d6cd8d671e14c735d051eff7140
  • 不,又是同样的事情
  • 看看这个:stackoverflow.com/questions/18478847/… 可能只是你的问题
  • 这为我指明了解决问题所需的方向。我提交了解决方案的答案。
【解决方案2】:

事实证明我过于复杂,因为通过 API 获取要点的详细信息还可以为您提供具有正确 url 的 comments_url 字段,因此无需拼接字符串,陷入下面@Zilvinas 提到的非常奇怪的问题。另外,对身体进行了微调

const body = { body: comment }

const config = {
    method: 'POST',
    headers: {
        'Authorization': credentials.authorizationHeader,
        'Content-Type': 'application/vnd.github.v3.text+json'
    },
    body: JSON.stringify(body)
};

修复了我得到的后续Problems parsing JSON 错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    • 1970-01-01
    • 2015-05-10
    • 2013-09-02
    • 2015-07-10
    • 2015-03-02
    相关资源
    最近更新 更多