How to retrieve list of public repos from user using Github's API
Example of doing this in Python using the requests library:
Prerequisite
Generate a personal access token on Github.
- Go to your Github account settings
- Select "Developer settings"
- Then "Personal access tokens" and
- Generate a new token with "repo" scope.
Using the API
Once you have your personal access token, you can use the following code to retrieve a list of all public repositories for a user:
import requests
username = "YOUR_GITHUB_USERNAME"
token = "YOUR_PERSONAL_ACCESS_TOKEN"
url = f"https://api.github.com/users/{username}/repos?type=public"
headers = {"Authorization": f"Token {token}"}
response = requests.get(url, headers=headers)
repos = response.json()
for repo in repos:
print(repo["name"])
Replace YOUR_GITHUB_USERNAME
with your Github username, and YOUR_PERSONAL_ACCESS_TOKEN
with the personal access token you generated.
This code sends a GET HTTP request to the Github API to retrieve a list of public repositories for the specified user. The response is in JSON format, which we can parse using the json()
method. The code then loops through the list of repositories and prints the name of each repository.