#100DaysOfCode in Python Transcripts
Chapter: Days 61-63: Using the Github API with Python
Lecture: Creating a gist with the Github API

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Alright, let's post to the GitHub API,
0:03 creating a gist.
0:05 Now, with the current access,
0:06 we cannot just post to the API.
0:09 It would mean not very secure, right?
0:11 So we need to create some sort of access token.
0:14 And there are various levels of access.
0:18 I mean the GitHub API is pretty granular.
0:20 But for this one,
0:21 I'm going to create a personal access token.
0:25 Let's go to my GitHub account,
0:28 settings,
0:32 developer settings,
0:35 personal access tokens,
0:37 and click on generate new token.
0:42 I'm just giving the description.
0:50 And look at all the scopes.
0:52 There's a lot of different access levels,
0:54 but I'm interested now in creating gist.
0:57 So I click on the corresponding access.
1:01 And I click on generate token.
1:05 That gives us our token.
1:06 And next I will show you
1:08 how to load that into the notebook.
1:10 As per best practice,
1:11 never leave such a token in your code.
1:14 But load it from the environment.
1:16 So back at the terminal,
1:19 deactivate the virtual environment,
1:21 open your venv virtual environment,
1:24 the name of your virtual environment.
1:25 venv, activate script with your favorite editor.
1:30 Go to the end,
1:31 and let's export
1:33 github_gist_create_token,
1:39 fit a token your just copy over.
1:42 Save this,
1:43 activate the virtual environment again.
1:47 Source or I have my alias set up.
1:53 And now I should have the variable in my environment.
1:58 Okay, great.
1:59 We can now post to the API.
2:02 Let's load in the token.
2:06 It would not be available
2:07 that would give me a keyerror.
2:08 So we are good.
2:10 Let's create a new object.
2:16 Passing in the token.
2:18 And to come back on those rate limits,
2:22 look at the difference.
2:26 Look at that.
2:27 Now we can make five thousand calls to the API.
2:30 Compare that to the, what was it?
2:32 50 or 60 before,
2:33 so authorizing with a token gives you more power.
2:37 Let's get my user.
2:42 And you see that I'm an authenticated user.
2:45 And now, let's create a gist.
2:52 Hold on there.
2:54 I'm missing required arguments.
2:56 Yeah, that's not going to work straight away, of course.
2:59 And I did that on purpose,
3:01 to go back to the help.
3:04 Look at that,
3:05 that's the contract or the interface we have
3:08 to this method of the API.
3:10 We defined if the gist is going to be public or not.
3:14 We give it files and a description.
3:17 And notice that the files need
3:19 to be of an input file contact type,
3:22 which I imported at the start.
3:24 So let's first, write some code to be posted.
3:29 So I have some code here,
3:31 that's actually the code we wrote before
3:33 to get the repo stats.
3:35 I'm not going to share this with the world.
3:37 Excitement!
3:38 So that would be me,
3:40 create, gist, public True.
3:45 And then we need to patch it
3:46 in a dictionary of the file name.
3:49 The name of the gist,
3:51 I'm going to call it,
3:53 repostats.py.
3:55 And the value would be that input file content object.
4:02 And I pass it in my code.
4:04 And lastly,
4:05 we give a description.
4:08 GitHub users most popular repos.
4:12 Alright, let's run this.
4:14 And it comes back with a gist.
4:17 Let's go over to GitHub.
4:24 And look at this.
4:25 There is a repo_stats.py,
4:29 which was just created by my user.
4:33 And look at that.
4:34 This code is now available to the world.
4:36 And it was automatically posted via the GitHub API,
4:40 using Python.