usrbinenv python3 sarathkaul on 141119 updated by lawric1 on 241120 authentication will be made via access token to generate your personal access token visit https github comsettingstokens note never hardcode any credential information in the code always use an environment file to store the private information and use the os module to get the information during runtime create a env file in the root directory and write these two lines in that file with your token usrbinenv bash export usertoken https docs github comenfreeproteamlatestrestreferenceusersgettheauthenticateduser https github comsettingstokens fetch github info of a user using the requests module usr bin env python3 sarathkaul on 14 11 19 updated by lawric1 on 24 11 20 authentication will be made via access token to generate your personal access token visit https github com settings tokens note never hardcode any credential information in the code always use an environment file to store the private information and use the os module to get the information during runtime create a env file in the root directory and write these two lines in that file with your token usr bin env bash export user_token https docs github com en free pro team latest rest reference users get the authenticated user https github com settings tokens fetch github info of a user using the requests module pragma no cover
from __future__ import annotations

import os
from typing import Any

import requests

BASE_URL = "https://api.github.com"


AUTHENTICATED_USER_ENDPOINT = BASE_URL + "/user"


USER_TOKEN = os.environ.get("USER_TOKEN", "")


def fetch_github_info(auth_token: str) -> dict[Any, Any]:
    
    headers = {
        "Authorization": f"token {auth_token}",
        "Accept": "application/vnd.github.v3+json",
    }
    return requests.get(AUTHENTICATED_USER_ENDPOINT, headers=headers).json()


if __name__ == "__main__":  
    if USER_TOKEN:
        for key, value in fetch_github_info(USER_TOKEN).items():
            print(f"{key}: {value}")
    else:
        raise ValueError("'USER_TOKEN' field cannot be empty.")