業界トップクラスの求人数を誇る転職エージェントPR

リクルートグループのコネクションを活かした非公開求人も充実、他にはない好条件の求人と出会える

4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

GitHub APIのレスポンスが 403 Forbidden になったら

Posted at

User-Agentヘッダーは設定していますか??
https://docs.github.com/en/rest/overview/resources-in-the-rest-api#user-agent-required

コード例

以下はissueを作成するコードです。
認証はパーソナルアクセストークンで行います。

PersonalAccessToken, organization, repositoryはご自身のものに置き換えてください。

github.ts
import https from 'https'

const createIssue = async (title: string, body: string, labels: string[]): Promise<number> => {
  return new Promise((resolve, reject) => {
    const req = https.request({
      method: 'POST',
      headers: {
        'User-Agent': 'YourApp', // これ大事
        'Accept': 'application/vnd.github.v3+json',
        'Content-Type': 'application/json',
        'Authorization': `token ${PersonalAccessToken}`
      },
      host: 'api.github.com',
      path: `/repos/${organization}/${repository}/issues`,
    }, (response) => {
      let res: any = ''

      response.on('data', (chunk) => {
        res += chunk
      })

      response.on('end', () => {
        res = JSON.parse(res)
        resolve(res.id)
      })

      response.on('error', (err: Error) => reject(err))
    })
    req.write(buildPayload(title, body, labels))

    req.on('error', (err: Error) => reject(err))
    req.end()
  })
}

const buildPayload = (title: string, body: string, labels: string[]): string => {
  return JSON.stringify({
    title, body, labels
  })
}

まとめ

全ての原因がこれに当てはまるかは分かりませんが、私はこれで解決できました。
@octokit/rest.jsなどを使った方が早いかもしれませんね)

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?