Authenticating With the GitHub API
00:00
Now you’ve created your personal access token, you can make requests to a GitHub API that accesses your profile’s information. You should start, as usual, by importing requests and saving the URL to the API as a variable.
00:13
This time you’re accessing the user API at https://api.github.com/user. Next, you should save your token as a variable. In future programs, it’s not a good idea to have your API key or token embedded in your code, as it’s not very secure.
00:33
But for this example, saving it as a variable is fine. Now you’re going to create a headers dictionary, much in the same way you created a query parameters dictionary previously.
00:45
This time, you only need one key, "Authorization", which must have a value with this specific format: "Bearer ", space, and then your token.
00:57
This is specific to GitHub, and it’s always worth checking out the documentation if you’re using a different API. Now you can include that headers dictionary with your request as an argument to the requests.get() function.
01:10
The response contains lots of information from your profile. For example, you can access the "name" and the "public_repos" key.
01:25 If you print those out and you see your information, then it means you’ve successfully used authentication.
01:36 Congratulations! Now you’ve learned about authentication, you’ve opened a whole world of new APIs to explore. Here’s a reminder of the authentication process.
01:46
You create an API key if you haven’t already got one, make a request in the usual way, include a headers dictionary with an "Authorization" key.
01:57
The value for that key must be "Bearer ", space, and then your token. It’s worthwhile noting that other APIs might ask for the token in a slightly different way, for example, in the query parameters instead of the headers.
02:10 But the process will be similar and will be explained in the documentation for the API. You’re probably very excited to send as many API requests as possible with your new skills, but you need to be careful.
02:22 If you send too many requests in a short space of time, you might get blocked. But don’t worry, I’ll show you a couple of methods of making sure you’re not putting too much demand on the APIs in the next lesson.
Become a Member to join the conversation.
