Search code examples
javascriptgithubpostgithub-api

GitHub API v3: Getting 404 error on creating issues


I'm following GitHub Docs try to get issue list and post issues.

I've managed to get the issue list using

GET https://api.github.com/repos/wheatup/wheatup.github.io/issues

But when I try to post an issue to the repo, I got a 404 error with following body:

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

Here's my post request:

URL

POST https://api.github.com/repos/wheatup/wheatup.github.io/issues

Headers

Accept: application/vnd.github.v3+json
Accept-Encoding: gzip, deflate, br
Accept-Language: en,zh-CN;q=0.9,zh;q=0.8,ja;q=0.7,zh-TW;q=0.6,sr;q=0.5,pl;q=0.4,la;q=0.3
Authorization: token d7fa1e545c*******************31957a97e06
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 79
Content-Type: application/json
DNT: 1
Host: api.github.com
Origin: http://localhost:3000
Pragma: no-cache
Referer: http://localhost:3000/
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: cross-site
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36

Body

{ 
  title: "Test", 
  body: "test content", 
  labels: [], 
  assignees: [], 
  milestone: 1
}

This is how I post the request:

const result = await axios.post('https://api.github.com/repos/wheatup/wheatup.github.io/issues', {
    title: 'Test',
    body: 'test content',
    labels: [],
    assignees: [],
    milestone: 1,
  }, {
    headers: {
      'Authorization': 'token d7fa1e545c*******************31957a97e06',
      'Content-Type': 'application/json',
      'Accept': 'application/vnd.github.v3+json'
    }
});

My repo is public, did I miss something? Any help would be greatly appreciated!


Solution

  • It seems you are missing the github token in the request. I get 404 on my local until I add the bearer token. Then I get 401 because I am not using an actual bearer token to hit your repo. So once you add that part, it all should work.

    Solution 1:

        const result = await axios.post('https://api.github.com/repos/wheatup/wheatup.github.io/issues', {
                title: 'Test',
                body: 'test content',
                // labels: [], --> Since empty, commented out as it is optional param
                // assignee: '', --> Since empty, commented out as it is optional param. Also you had a typo and this attributes expects string not array
                milestone: 1,
            }, {
            headers: {
               'Authorization': `Bearer ${githubToken}`,
               'Content-Type': 'application/json',
               'Accept': 'application/vnd.github.v3+json'
            }
        });
    

    When dealing with github API, I would suggest use their toolkit instead because you only need to provide the token once and then subsequent request can just have the data provided to them

    Alternative Solution 2: --> This only applies to not have to deal with passing bearer token on every request so ignore if you rather keep using axios.

      const octokit = new Octokit({ auth: githubToken });
    
      const response = await octokit.request('POST /repos/{owner}/{repo}/issues', {
           owner: 'wheatup',
           repo: 'wheatup.github.io',
           title: 'Test',
           body: 'test content',
           milestone: 1,
      });
    

    EDIT

    Solution 3: -> Actual solution to the problem provided above

    When dealing with OAuth apps there are some steps to take

    1. Users are redirected to request their GitHub identity
    2. Users are redirected back to your site by GitHub
    3. Your app accesses the API with the user's access token

    NOTE: When calling the request identity make sure to require the scope necessary for the API calls to make. In this case the 404 was received due to token not having proper permissions to the repo as scope was missing.

    More information about oauth API calls can be found here https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps/