pythongithubpipgithub-apipygithub

Download the latest relase file with token from private repo


I want to download files from my private repository using python.

What should I do?

I tried something like this, but it doesn't work. I also allowed everything in the token for testing, I get error again.

import requests

headers = {'Authorization': 'token ' + 'ghp_xxxxxxxxxxxxxxx'}

def download_and_apply_update(self, loading_window):
        download_url = "https://github.com/HamzaYslmn/example/releases/latest/download/example.exe"
        new_executable_path = "DetaBaseFatura_New.exe"
    
        response = requests.get(download_url, stream=True , headers=headers)
        total_size = int(response.headers.get("content-length", 0))
        chunk_size = 8192  # You can adjust this value as needed
    
        with open(new_executable_path, "wb") as new_executable_file:
            downloaded_size = 0
            for data in response.iter_content(chunk_size=chunk_size):
                new_executable_file.write(data)
                downloaded_size += len(data)
                progress_percentage = (downloaded_size / total_size) * 100
                loading_window[1]["text"] = f"Progress: {progress_percentage:.2f}%"
                loading_window[2]["value"] = progress_percentage
                loading_window[0].update()
    

this way I can access the tag, for example. But downloading is a completely different matter.

def get_latest_version_from_github(self):
        latest_tag_url = "https://api.github.com/repos/HamzaYslmn/example/releases/latest"
        response = requests.get(latest_tag_url, headers=headers)
        response_data = response.json()
        print(response_data)
        latest_tag = response_data['tag_name']
        
        return latest_tag

Solution

  • latest_tag_url = "https://api.github.com/repos/HamzaYslmn/{REPO}/releases/latest"
            headers = {
                "Accept": "application/vnd.github+json",
                "Authorization": "Bearer ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
                "X-GitHub-Api-Version": "2022-11-28"
            }
            response = requests.get(latest_tag_url, headers=headers)
    

    I solved