👤

【GitHub API】GITHUB_IDからuser情報を取得する方法

2023/04/14に公開
  • FirebaseAuthでGithubログインした後に取れる情報はGITHUB_IDである
  • GitHubのusernameはいつでも変えることができるので一意に管理することができない→GITHUB_IDを使いたい

という理由があり、GITHUB_IDからuser情報を取得する方法をいろいろ調べました。

公式ドキュメントには、その方法が書いてない

GitHub APIからuserを取得するときに、ドキュメントだと、api.github.com/users/{GITHUB_USERNAME} っていうエンドポイントしかありません。

https://docs.github.com/en/rest/users/users?apiVersion=2022-11-28#get-a-user

結論:方法はある

公式ドキュメントだとないんですが、stackoverflowやGPT-4によると、 api.github.com/user/{GITHUB_ID} というエンドポイントでuserを取得することができます。

めちゃくちゃ似てるんですが、usersじゃなくてuserというエンドポイントです。

参照元

stackoverflow

https://stackoverflow.com/a/69785405

GPT-4

質問
Are there two API to get user data?
Are the below correct?

- https://api.github.com/user/{GITHUB_ID}
- https://api.github.com/users/{GITHUB_USERNAME}
回答
Yes, you are correct. There are two separate APIs to get user data from the GitHub REST API:

1. Get user data by GitHub ID:
Endpoint: https://api.github.com/user/{GITHUB_ID}
Replace {GITHUB_ID} with the user's GitHub ID.

2. Get user data by GitHub username:
Endpoint: https://api.github.com/users/{GITHUB_USERNAME}
Replace {GITHUB_USERNAME} with the user's GitHub username.

Both of these endpoints return similar user data, but they use different identifiers (ID or username) to fetch the information. Keep in mind that the GitHub ID is a numeric identifier, while the GitHub username is a string identifier.

↓関係ないのも混じってて冗長です。
https://sharegpt.com/c/tQ0aiwQ

2つのuser取得方法がある

ということで、GPT言ってることマジか??って疑いつつも、ちゃんと動いたので、正しそうです。

という2つのAPIがあって 、今回は前者の方を使って実装を進めることができました。

しかし、teamへの追加やorganizationへの招待、解除はGITHUB_IDではできない

見出しの通りなので、https://api.github.com/user/{GITHUB_ID} で取得した後は、以下のような形式のJSONからloginというprameterを取り出して先ほどでいう {GITHUB_USERNAME} として使ってください。

{
  "login": "octocat",
  "id": 1,
  "name": "monalisa octocat",
  "email": "octocat@github.com",
  "created_at": "2008-01-14T04:33:35Z",
  "updated_at": "2008-01-14T04:33:35Z"
}
Flutter大学

Discussion