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


Talk Python's Mastodon Michael Kennedy's Mastodon