the_endian
the_endian

Reputation: 2527

How can I pull private repo data using GitHub API?

I am a member of an organization which has a lot of private repositories on GitHub. I need to use the GitHub API to get data from the private repositories. I am using the following python code:

url = "https://api.github.com/orgs/myorg/repos?access_token=[mytokenhere]"
headers = {"Authorization": "token [myPATHhere]"}
session = requests.Session()
response = session.get(url, headers = headers)
content = response.text

my_json = json.loads(response.text)


for item in my_json:
    print(item['html_url'])

Where PAT = my access token. This still only returns the public repositories. I've seen the related question but the listed solutions do not solve my problem. Note that I've authorized my access token to get into the private repository. I've also tried api.github.com/users/repos and api.github.com/users/[myorghere] and still no private repos are returned. Am I not submitted the token correctly?

Upvotes: 8

Views: 3280

Answers (2)

Ayanabha Pramanik
Ayanabha Pramanik

Reputation: 1

Maybe your code would work for an older version of the REST API. However, when I tried accessing Github private repositories using the following code, it worked.

import requests

data = {"username": ""} # Put your username in the placeholder.
refresh_url = "https://api.github.com/user/repos"  # dont change, else error.
headers = {
    "Accept": "application/json",
    "Authorization": "Bearer TOKEN" #replace your token, and give necessary 
                                    # permission
}
user_data = requests.get(url = refresh_url, headers= headers, params=data)
# print(user_data.status_code)
# print(user_data.json()[0])
for repository in user_data.json():
    print(str(repository["full_name"]) + " -> " + str(repository["private"]))

Upvotes: 0

runnerpaul
runnerpaul

Reputation: 7236

Go into your personal access token and ensure that you have selected the necessary scopes. The first one is for private repos so you may need to select some/all of the options there.

Upvotes: 4

Related Questions