Skip to content

Commit 790acec

Browse files
Update github.py
1 parent 0ff377b commit 790acec

File tree

1 file changed

+64
-31
lines changed

1 file changed

+64
-31
lines changed

consuming-apis-python/github.py

Lines changed: 64 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,87 @@
11
import requests
22

3-
# Replace the CLIENT_ID and CLIENT_SECRET below with your own.
4-
client_id = "CLIENT_ID"
5-
client_secret = "CLIENT_SECRET"
6-
redirect_uri = "https://httpbin.org/anything"
7-
oauth_base_url = "https://github.com/login/oauth"
8-
api_base_url = "https://api.github.com"
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
95

106

11-
def get_code():
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 "Authorization callback URL" field
12+
REDIRECT_URI = "<REPLACE_WITH_REDIRECT_URI>"
13+
14+
# In this method you'll ask the Github API for a URL to redirect the user for authentication
15+
def create_oauth_link():
1216
params = {
13-
"client_id": client_id,
14-
"redirect_uri": redirect_uri,
17+
"client_id": CLIENT_ID,
18+
"redirect_uri": REDIRECT_URI,
1519
"scope": "user",
1620
"response_type": "code",
1721
}
1822

19-
endpoint = oauth_base_url + "/authorize"
20-
url = requests.get(endpoint, params=params).url
21-
print("Go to this URL and follow the OAuth flow there: %s" % url)
22-
code = input("code: ")
23-
return code
23+
# This endpoint is defined in the Github documentation:
24+
# https://docs.github.com/en/free-pro-team@latest/developers/apps/authorizing-oauth-apps#1-request-a-users-github-identity
25+
endpoint = "https://github.com/login/oauth/authorize"
26+
27+
response = requests.get(endpoint, params=params)
28+
29+
# When you make the request above, Github will redirect you to their website to input your credentials. Since you're doing
30+
# this programmatically, you need to get the `url` parameter and print it in the console instead.
31+
url = response.url
2432

33+
return url
2534

26-
def get_token(code=None):
27-
endpoint = oauth_base_url + "/access_token"
35+
# In this method you'll exchange the code you got from the Github API with an access token
36+
def exchange_code_for_access_token(code=None):
2837
params = {
29-
"client_id": client_id,
30-
"client_secret": client_secret,
31-
"redirect_uri": redirect_uri,
38+
"client_id": CLIENT_ID,
39+
"client_secret": CLIENT_SECRET,
40+
"redirect_uri": REDIRECT_URI,
3241
"code": code,
3342
}
43+
44+
# Here you define the content type you're expecting to get. In this case – JSON.
3445
headers = {"Accept": "application/json"}
35-
response = requests.post(endpoint, data=params, headers=headers).json()
36-
return response["access_token"]
3746

3847

39-
def get_user(token=None):
40-
headers = {"Authorization": "token %s" % token}
48+
# This endpoint is defined in the Github documentation:
49+
# https://docs.github.com/en/free-pro-team@latest/developers/apps/authorizing-oauth-apps#2-users-are-redirected-back-to-your-site-by-github
50+
endpoint = "https://github.com/login/oauth/access_token"
4151

52+
response = requests.post(endpoint, params=params, headers=headers).json()
53+
return response["access_token"]
54+
55+
# Finally in this method you'll print the user information re. its name, username and number of private repositories.
56+
def print_user_info(access_token=None):
57+
# Now you need to send the `access_token` in the headers when calling the API
58+
headers = {"Authorization": f"token {access_token}"}
59+
60+
# This endpoint is defined in the Github documentation:
61+
# https://docs.github.com/en/free-pro-team@latest/rest/reference/users#get-the-authenticated-user
4262
endpoint = "https://api.github.com/user"
63+
4364
response = requests.get(endpoint, headers=headers).json()
44-
return response
4565

66+
# The response will be a dictionary with multiple user-related field. You can try `print(response)` to see all of them.
67+
name = response["name"]
68+
username = response["login"]
69+
private_repos_count = response["total_private_repos"]
70+
print(
71+
f"{name} ({username}) | number of private repositories: {private_repos_count}"
72+
)
73+
74+
# So, one last time, step by step:
75+
# 1. Create a link to redirect the user to for authentication:
76+
link = create_oauth_link()
77+
print(f"Follow the link to start the authentication with Github: {link}")
78+
79+
# 2. Paste the code you got from Github after authenticating
80+
code = input("Github code: ")
4681

47-
custom_code = get_code()
48-
access_token = get_token(code=custom_code)
49-
user = get_user(token=access_token)
82+
# 3. Exchange that code with an access token
83+
access_token = exchange_code_for_access_token(code)
84+
print(f"Exchanged code {code} with access token: {access_token}")
5085

51-
name = user["name"]
52-
username = user["login"]
53-
private_repos_count = user["total_private_repos"]
54-
print("%s (%s) | private repos: %s" % (name, username, private_repos_count))
86+
# 4. Fetch user information
87+
print_user_info(access_token=access_token)

0 commit comments

Comments
 (0)