Getting began with Python APIs


Interacting with GitHub

We will use APIs to work together with GitHub too. Conserving it easy, we’ll use the POST request to create a brand new repo known as api_test.

Authorization

Advertisement

First, as all the time, we want an authorization token. Luckily, we are able to get this arrange a lot sooner than we did with the Google Maps API — you’ll find a step-by-step guide here.

Observe that on step seven, it’s totally as much as you which ones permissions you choose — however all we are going to want is repo:

Screenshot during step seven of GitHub authorization token showing repo to be checked, and delete repo unchecked.
We solely want repo permissions for this. I’ve averted ticking delete_repo — it looks like a foul concept

Now, we are able to authenticate ourselves by including headers= to our requests.

Advertisement

Create a repo

To create a repo, we merely ship a POST request to https://api.github.com/ person/repos. The one required subject right here is the identify worth, which specifies the brand new repo identify. We additionally set public to true which merely units the repo privateness settings to public — relatively than non-public (the default).

Observe that we use PAYLOAD to comprise our directions for the API, which we move to the knowledge argument inside requests.publish. We additionally embody our authorization key/token utilizing the headers argument.

Advertisement

Outcomes

If we reach creating the repo, we are going to obtain code 201 Created — indicating that the request has succeeded, and we now have created a brand new useful resource.

Image for post

We will additionally examine on GitHub, and certain sufficient the api_test repo is true there:

Advertisement
Image for post

The GitHub API could be very cool, there’s lots we are able to do with it, and due to that, it’s nice for studying — I’d advocate looking at the docs when you’d prefer to study extra (be happy to ask me questions too!).



Advertisement
Download Premium WordPress Themes Free