この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
こんにちは、CX事業本部 IoT事業部の若槻です。
今回は、GitHub API v3でPOSTリクエストをする際はPublic Repositoryであっても権限(アクセストークン)が必要だった、という話です。
Public RepositoryでAPIの動作確認をしたい
GitHub API v3でIssue周りの動作を確認するために、次のoctocat/Hello-World
Repositoryを使用した際のことです。
octocat/Hello-World
はGitHub公式でホストしているRepositoryで、APIドキュメントでの利用例としても使われています。
Issueの取得は可能
まずAPIでIssue取得を行ってみました。
コンソールから見てみるとすでにいろんな人がテスト的にIssueを作成していますね。
Issueの取得(List)には次のエンドポイントを使用します。
GET /repos/{owner}/{repo}/issues
curlでGETリクエストをしてみます。すると実行結果でIssueがリストで取得できました。
curl \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/octocat/hello-world/issues
[
{
"url": "https://api.github.com/repos/octocat/Hello-World/issues/2083",
"repository_url": "https://api.github.com/repos/octocat/Hello-World",
"labels_url": "https://api.github.com/repos/octocat/Hello-World/issues/2083/labels{/name}",
"comments_url": "https://api.github.com/repos/octocat/Hello-World/issues/2083/comments",
"events_url": "https://api.github.com/repos/octocat/Hello-World/issues/2083/events",
"html_url": "https://github.com/octocat/Hello-World/issues/2083",
"id": 1087937918,
"node_id": "I_kwDOABPHjc5A2J1-",
"number": 2083,
"title": "creating an issue",
"user": {
"login": "oJordany",
"id": 84668196,
…
Issueの投稿は失敗してしまう
次にPOSTリクエストを試してみようということでIssueの投稿をしてみました。
Issueの投稿には次のエンドポイントを使用します。
POST /repos/{owner}/{repo}/issues
curlでPOSTリクエストをしてみます。しかし実行結果はNot Found
となりました。
$ curl \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/octocat/hello-world/issues \
-X POST \
-d '{"title": "wakatsuki test"}'
{
"message": "Not Found",
"documentation_url": "https://docs.github.com/rest/reference/issues#create-an-issue"
}
解決
結論から言うと、POSTする際はPublic Repositoryであっても権限(アクセストークン)が必要でした。
GitHubアカウントのアクセストークンを作成します。必要な権限スコープはpublic_repo
です。
アクセストークンをAuthorizationヘッダーに指定してPOSTリクエストをしてみます。すると投稿結果が返ってきて実行が成功したようです!
% GH_ACCESS_TOKEN=${##アクセストークン##}
% curl \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token ${GH_ACCESS_TOKEN}" \
https://api.github.com/repos/octocat/hello-world/issues \
-X POST \
-d '{"title": "wakatsuki test"}'
{
"url": "https://api.github.com/repos/octocat/Hello-World/issues/2084",
"repository_url": "https://api.github.com/repos/octocat/Hello-World",
"labels_url": "https://api.github.com/repos/octocat/Hello-World/issues/2084/labels{/name}",
"comments_url": "https://api.github.com/repos/octocat/Hello-World/issues/2084/comments",
"events_url": "https://api.github.com/repos/octocat/Hello-World/issues/2084/events",
"html_url": "https://github.com/octocat/Hello-World/issues/2084",
"id": 1089951359,
"node_id": "I_kwDOABPHjc5A91Z_",
"number": 2084,
"title": "wakatsuki test",
"user": {
"login": "cm-rwakatsuki",
"id": 57384023,
"node_id": "MDQ6VXNlcjU3Mzg0MDIz",
"avatar_url": "https://avatars.githubusercontent.com/u/57384023?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/cm-rwakatsuki",
"html_url": "https://github.com/cm-rwakatsuki",
"followers_url": "https://api.github.com/users/cm-rwakatsuki/followers",
"following_url": "https://api.github.com/users/cm-rwakatsuki/following{/other_user}",
"gists_url": "https://api.github.com/users/cm-rwakatsuki/gists{/gist_id}",
"starred_url": "https://api.github.com/users/cm-rwakatsuki/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/cm-rwakatsuki/subscriptions",
"organizations_url": "https://api.github.com/users/cm-rwakatsuki/orgs",
"repos_url": "https://api.github.com/users/cm-rwakatsuki/repos",
"events_url": "https://api.github.com/users/cm-rwakatsuki/events{/privacy}",
"received_events_url": "https://api.github.com/users/cm-rwakatsuki/received_events",
"type": "User",
"site_admin": false
},
"labels": [
],
"state": "open",
"locked": false,
"assignee": null,
"assignees": [
],
"milestone": null,
"comments": 0,
"created_at": "2021-12-28T14:58:24Z",
"updated_at": "2021-12-28T14:58:24Z",
"closed_at": null,
"author_association": "NONE",
"active_lock_reason": null,
"body": null,
"closed_by": null,
"reactions": {
"url": "https://api.github.com/repos/octocat/Hello-World/issues/2084/reactions",
"total_count": 0,
"+1": 0,
"-1": 0,
"laugh": 0,
"hooray": 0,
"confused": 0,
"heart": 0,
"rocket": 0,
"eyes": 0
},
"timeline_url": "https://api.github.com/repos/octocat/Hello-World/issues/2084/timeline",
"performed_via_github_app": null
}
コンソールからもIssueが投稿されていることが確認できます!
おわりに
GitHub API v3でPOSTリクエストをする際はPublic Repositoryであっても権限(アクセストークン)が必要だった、という話でした。
すべてのエンドポイントで試したわけではありませんが、「GETはうまく動くがPOSTは動かない」という際にはアクセストークンを使用するとうまく動くかもしれません。
以上