1

I've created a private repo on Github and need to access a .txt file from the repo in a Python script I am writing. I was able to access the .txt file using the https://raw.githubusercontent.com/ url which has a token at the end, but the token changes every few minutes or so, meaning I cannot access the contents of the file if I run my script after a few minutes (I get a 404: Not Found). Is there a way to access the contents of the file without using the raw version of it? I have the personal access token for the repo and am trying to figure out a way to get the contents of the .txt through that but have thus far been unsuccessful.

Here's what I have tried so far:

user='userName'
pao='github_pat_123456789abcdefg'
github_session = requests.Session()
github_session.auth = (user, pao)
f = "https://raw.githubusercontent.com/.../file.txt"
req = github_session.get(f).text

But I still get the 404 error. Is there anyway to consistently access the contents of the .txt file?

prismarine
  • 15
  • 1
  • 3

1 Answers1

0

For private repo:

import requests

def get_file_contents(repo_url, file_path, access_token):
    # Construct the API URL to access the file content
    api_url = f"{repo_url}/contents/{file_path}"

    # Send a GET request to the API with the access token
    headers = {
        "Authorization": f"token {access_token}",
        "Accept": "application/vnd.github.v3+json"
    }
    response = requests.get(api_url, headers=headers)

    if response.status_code == 200:
        # The response contains the file content in Base64 encoding.
        # You may want to decode it to get the actual content.
        content = response.json().get("content", "")
        decoded_content = base64.b64decode(content).decode("utf-8")
        return decoded_content
    else:
        print(f"Failed to retrieve file. Status Code: {response.status_code}")
        return None

if __name__ == "__main__":
    # Replace these with your specific repository URL, file path, and access token
    repo_url = "https://api.github.com/repos/<username>/<repo>"
    file_path = "path/to/your/file.txt"
    access_token = "YOUR_PERSONAL_ACCESS_TOKEN"

    content = get_file_contents(repo_url, file_path, access_token)
    if content:
        print(content)

Go to your GitHub account settings. Navigate to "Developer settings" -> "Personal access tokens." Click "Generate new token." Give the token an appropriate name, select the desired permissions (usually, "repo" scope is sufficient for private repository access), and click "Generate token." Copy the generated token and use it in the access_token variable in the Python program

Public repo: If you want to access .txt files from a public Github repository without cloning or downloading the files, you can use the Github API to fetch the contents of the files directly. One way to achieve this is by using the requests library in Python. Here's a simple example of how to do it:

import requests

def get_file_contents(repo_url, file_path):
    # Construct the API URL to access the file content
    api_url = f"{repo_url}/contents/{file_path}"

    # Send a GET request to the API
    response = requests.get(api_url)

    if response.status_code == 200:
        # The response contains the file content in Base64 encoding.
        # You may want to decode it to get the actual content.
        content = response.json().get("content", "")
        decoded_content = base64.b64decode(content).decode("utf-8")
        return decoded_content
    else:
        print(f"Failed to retrieve file. Status Code: {response.status_code}")
        return None

if __name__ == "__main__":
    # Replace these with your specific repository URL and file path
    repo_url = "https://api.github.com/repos/<username>/<repo>"
    file_path = "path/to/your/file.txt"

    content = get_file_contents(repo_url, file_path)
    if content:
        print(content)

Note: In the repo_url, replace with the owner's GitHub username and with the repository name.

Please ensure you have the requests library installed (pip install requests) before running the above code. Also, remember to replace the placeholders in the repo_url with the appropriate values.

This approach allows you to fetch the contents of the .txt file directly from the public GitHub repository without cloning or downloading the entire repository. However, if the repository becomes private or requires authentication in the future, this method will no longer work.

vishwa
  • 54
  • 6