88CLIENT_ID = "<REPLACE_WITH_CLIENT_ID>"
99CLIENT_SECRET = "<REPLACE_WITH_CLIENT_SECRET>"
1010
11- # REPLACE the following variable with what you added in the "Authorization callback URL" field
11+ # REPLACE the following variable with what you added in the
12+ # "Authorization callback URL" field.
1213REDIRECT_URI = "<REPLACE_WITH_REDIRECT_URI>"
1314
14- # In this method you'll ask the Github API for a URL to redirect the user for authentication
15+
16+ # In this method you'll ask the Github API for a URL to redirect the user
17+ # for authentication.
1518def create_oauth_link ():
1619 params = {
1720 "client_id" : CLIENT_ID ,
@@ -25,14 +28,18 @@ def create_oauth_link():
2528 endpoint = "https://github.com/login/oauth/authorize"
2629
2730 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+
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.
3136 url = response .url
3237
3338 return url
3439
35- # In this method you'll exchange the code you got from the Github API with an access token
40+
41+ # In this method you'll exchange the code you got from the Github API with
42+ # an access token.
3643def exchange_code_for_access_token (code = None ):
3744 params = {
3845 "client_id" : CLIENT_ID ,
@@ -41,36 +48,42 @@ def exchange_code_for_access_token(code=None):
4148 "code" : code ,
4249 }
4350
44- # Here you define the content type you're expecting to get. In this case – JSON.
51+ # Here you define the content type you're expecting to get.
52+ # In this case – JSON.
4553 headers = {"Accept" : "application/json" }
4654
47-
4855 # This endpoint is defined in the Github documentation:
4956 # https://docs.github.com/en/free-pro-team@latest/developers/apps/authorizing-oauth-apps#2-users-are-redirected-back-to-your-site-by-github
5057 endpoint = "https://github.com/login/oauth/access_token"
5158
5259 response = requests .post (endpoint , params = params , headers = headers ).json ()
5360 return response ["access_token" ]
5461
55- # Finally in this method you'll print the user information re. its name, username and number of private repositories.
62+
63+ # Finally in this method you'll print the user information re. its name,
64+ # username and number of private repositories.
5665def print_user_info (access_token = None ):
57- # Now you need to send the `access_token` in the headers when calling the API
66+ # Now you need to send the `access_token` in the headers
67+ # when calling the API.
5868 headers = {"Authorization" : f"token { access_token } " }
59-
69+
6070 # This endpoint is defined in the Github documentation:
6171 # https://docs.github.com/en/free-pro-team@latest/rest/reference/users#get-the-authenticated-user
6272 endpoint = "https://api.github.com/user"
63-
73+
6474 response = requests .get (endpoint , headers = headers ).json ()
6575
66- # The response will be a dictionary with multiple user-related field. You can try `print(response)` to see all of them.
76+ # The response will be a dictionary with multiple user-related field.
77+ # You can try `print(response)` to see all of them.
6778 name = response ["name" ]
6879 username = response ["login" ]
6980 private_repos_count = response ["total_private_repos" ]
7081 print (
71- f"{ name } ({ username } ) | number of private repositories: { private_repos_count } "
82+ f"{ name } ({ username } ) | "
83+ f"number of private repositories: { private_repos_count } "
7284 )
7385
86+
7487# So, one last time, step by step:
7588# 1. Create a link to redirect the user to for authentication:
7689link = create_oauth_link ()
0 commit comments