8

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?

1

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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"]))

1 Comment

Commenting to say that I tested and verified the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.