|
| 1 | +import requests |
| 2 | + |
| 3 | +# First, make sure to follow the steps to create your own GitHub application: |
| 4 | +# https://docs.github.com/en/free-pro-team@latest/developers/apps/creating-an-oauth-app |
| 5 | + |
| 6 | + |
| 7 | +# REPLACE the following variables with your Client ID and Client Secret |
| 8 | +CLIENT_ID = "<REPLACE_WITH_CLIENT_ID>" |
| 9 | +CLIENT_SECRET = "<REPLACE_WITH_CLIENT_SECRET>" |
| 10 | + |
| 11 | +# REPLACE the following variable with what you added in the |
| 12 | +# "Authorization callback URL" field. |
| 13 | +REDIRECT_URI = "<REPLACE_WITH_REDIRECT_URI>" |
| 14 | + |
| 15 | + |
| 16 | +# In this method you'll ask the GitHub API for a URL to redirect the user |
| 17 | +# for authentication. |
| 18 | +def create_oauth_link(): |
| 19 | + params = { |
| 20 | + "client_id": CLIENT_ID, |
| 21 | + "redirect_uri": REDIRECT_URI, |
| 22 | + "scope": "user", |
| 23 | + "response_type": "code", |
| 24 | + } |
| 25 | + |
| 26 | + # This endpoint is defined in the GitHub documentation: |
| 27 | + # https://docs.github.com/en/free-pro-team@latest/developers/apps/authorizing-oauth-apps#1-request-a-users-github-identity |
| 28 | + endpoint = "https://github.com/login/oauth/authorize" |
| 29 | + |
| 30 | + response = requests.get(endpoint, params=params) |
| 31 | + |
| 32 | + # When you make the request above, GitHub will redirect you to their |
| 33 | + # website to input your credentials. Since you're doing this |
| 34 | + # programmatically, you need to get the `url` parameter and print it in |
| 35 | + # the console instead. |
| 36 | + url = response.url |
| 37 | + |
| 38 | + return url |
| 39 | + |
| 40 | + |
| 41 | +# In this method you'll exchange the code you got from the GitHub API with |
| 42 | +# an access token. |
| 43 | +def exchange_code_for_access_token(code=None): |
| 44 | + params = { |
| 45 | + "client_id": CLIENT_ID, |
| 46 | + "client_secret": CLIENT_SECRET, |
| 47 | + "redirect_uri": REDIRECT_URI, |
| 48 | + "code": code, |
| 49 | + } |
| 50 | + |
| 51 | + # Here you define the content type you're expecting to get. |
| 52 | + # In this case – JSON. |
| 53 | + headers = {"Accept": "application/json"} |
| 54 | + |
| 55 | + # This endpoint is defined in the GitHub documentation: |
| 56 | + # https://docs.github.com/en/free-pro-team@latest/developers/apps/authorizing-oauth-apps#2-users-are-redirected-back-to-your-site-by-github |
| 57 | + endpoint = "https://github.com/login/oauth/access_token" |
| 58 | + |
| 59 | + response = requests.post(endpoint, params=params, headers=headers).json() |
| 60 | + return response["access_token"] |
| 61 | + |
| 62 | + |
| 63 | +# Finally in this method you'll print the user information re. its name, |
| 64 | +# username and number of private repositories. |
| 65 | +def print_user_info(access_token=None): |
| 66 | + # Now you need to send the `access_token` in the headers |
| 67 | + # when calling the API. |
| 68 | + headers = {"Authorization": f"token {access_token}"} |
| 69 | + |
| 70 | + # This endpoint is defined in the GitHub documentation: |
| 71 | + # https://docs.github.com/en/free-pro-team@latest/rest/reference/users#get-the-authenticated-user |
| 72 | + endpoint = "https://api.github.com/user" |
| 73 | + |
| 74 | + response = requests.get(endpoint, headers=headers).json() |
| 75 | + |
| 76 | + # The response will be a dictionary with multiple user-related field. |
| 77 | + # You can try `print(response)` to see all of them. |
| 78 | + name = response["name"] |
| 79 | + username = response["login"] |
| 80 | + private_repos_count = response["total_private_repos"] |
| 81 | + print( |
| 82 | + f"{name} ({username}) | " |
| 83 | + f"number of private repositories: {private_repos_count}" |
| 84 | + ) |
| 85 | + |
| 86 | + |
| 87 | +# So, one last time, step by step: |
| 88 | +# 1. Create a link to redirect the user to for authentication: |
| 89 | +link = create_oauth_link() |
| 90 | +print(f"Follow the link to start the authentication with GitHub: {link}") |
| 91 | + |
| 92 | +# 2. Paste the code you got from GitHub after authenticating |
| 93 | +code = input("GitHub code: ") |
| 94 | + |
| 95 | +# 3. Exchange that code with an access token |
| 96 | +access_token = exchange_code_for_access_token(code) |
| 97 | +print(f"Exchanged code {code} with access token: {access_token}") |
| 98 | + |
| 99 | +# 4. Fetch user information |
| 100 | +print_user_info(access_token=access_token) |
0 commit comments