ME204 – Data Engineering for the Social World
23 July 2025
This deck contains copy-pasteable patterns for common API authentication methods.
💡 Purpose: Use these slides as a reference when working with APIs that require authentication. Each pattern can be adapted for different services.
Think of APIs as developers of a company giving you explicit permission to access their data, provided you adhere to their rules.
Another Real-World Analogy (than the one about a restaurant)
Library System:
API System:
REST = REpresentational State Transfer
What it means in practice:
GET
, POST
, PUT
, DELETE
)/users/123
, /posts/456
)Example REST URL patterns:
GET /users/johndoe → Get user info
GET /users/johndoe/posts → Get user's posts
POST /posts → Create new post
PUT /posts/123 → Update post 123
DELETE /posts/123 → Delete post 123
Examples:
Authorization: Bearer abc123
Content-Type: application/json
User-Agent: MyApp/1.0
?
Examples:
?limit=10&sort=date&category=news
Obviously: read the docs of the API you’re using to understand it!
So far we’ve only seen query parameters (in the URL). APIs use different parameter types:
# Visible in URL after ?
params = {"limit": 10, "sort": "date"}
response = requests.get(url, params=params)
# Results in: https://api.example.com/data?limit=10&sort=date
# Sent in request body, like a web form
data = {"username": "john", "message": "Hello world"}
response = requests.post(url, data=data)
User-Agent tells the API what application (WHO) is making the request.
Examples of User-Agent strings:
Chrome browser:
Mozilla/5.0 (Windows NT 10.0; Win64; x64)
AppleWebKit/537.36 Chrome/91.0.4472.124
Python requests:
python-requests/2.28.1
Your custom app:
MyWeatherApp/1.0 (contact@example.com)
Some APIs may require you to identify yourself more explicitly in the user agent header.
From Wikimedia API (the organisation behind Wikipedia):
Requests (e.g. from browsers or scripts) that do not send a descriptive User-Agent header, may encounter an error message like this:
Scripts should use an informative User-Agent string with contact information, or they may be blocked without notice.
They ask people to use:
User-Agent: CoolBot/0.0 (https://example.org/coolbot/; coolbot@example.org) generic-library/0.0
That is:
<client name>/<version> (<contact information>) <library/framework name>/<version> [<library name>/<version> ...]
“Parts that are not applicable can be omitted.”
You can see the actual HTTP requests your browser makes:
You’ll see:
Try This Now!
headers = {
"Authorization": "Bearer token123",
"User-Agent": "ME204-Project/1.0",
"Accept": "application/json"
}
response = requests.get(url, headers=headers)
It could look like this:
Once we start connecting to new APIs, we will need to register our “app” with the API provider.
📱 “App” = Your Registered Application/Client
When you sign up for API access, you create an “app” which is just:
Think of it as registering your code project with the API service, not building a mobile app!
🕵️♂️ I will ask you to be good at hiding your API secrets!
(from unintended eyes)
What Happens If You Don’t Hide Secrets?
If you commit API keys to GitHub:
Real example: AWS bills for thousands of dollars from stolen keys!
.env
File SolutionProblem: You need credentials in your code, but can’t commit them to GitHub.
Solution: Store them in a .env
file that stays on your computer only.
.env
file (never commit this!).gitignore
file# Never commit these files
.env
*.env
.env.local
.env.production
# Also ignore common sensitive files
config.json
secrets.txt
credentials.csv
Before Every Commit
.env
file exists and contains your secrets.gitignore
includes .env
git status
- make sure .env
is NOT listedos.getenv()
, not hardcoded keysRemember: Once something is on GitHub, it’s potentially public forever!
Now that you understand the security principles, let’s set up Reddit API access together.
🎯 Follow along with these steps:
Create a Reddit account (if you don’t have one): reddit.com/register
Create a Reddit “app” for API access: reddit.com/prefs/apps
Follow these setup steps:
ME204_2025_YourName
(e.g., ME204_2025_JonCardoso
)LSE ME204 Course - Reddit API Practice
http://localhost:8000
(required, but won’t be used)After creating your app, you’ll see a screen like this:
📝 Write down these 4 pieces of information:
Critical Security Reminder
NEVER put these credentials directly in your code or notebooks!
We’ll create a .env
file in the next step to store them securely.
Now we’ll switch to hands-on coding!
What We’ll Do Next
ME204_W02D03_lecture.ipynb
.env
file with your Reddit credentialsme204-study-notes
repository.gitignore
protects your .env
fileLet’s code together! 💻
[This section will be covered after the hands-on work]
Increasing Security & Complexity →
Google Maps Geocoding API: Part of Google Maps API that converts addresses to coordinates and vice versa.
Key looks like this: AIzaSyDaGmWKa4JsXZ-HjGw7ISLn_3namBGewQe
(see Geocoding API docs and their page about API keys).
OpenWeather API: Simple weather data access
Key looks like this: 1234567890abcdef
(see OpenWeather API docs and their page about API keys).
Advantages
Limitations
If you need to work with an API that requires an API key, you will likely need code that looks like this:
They also warn about API keys on their page!
Advantages
Limitations
If you encounter an API that requires Basic Authentication, you will likely need code that looks like this:
Advantages
Limitations
If you encounter an API that requires Bearer Token authentication, you will likely need code that looks like this:
Advantages
Limitations
response = requests.get(url, headers=headers)
# This will raise an exception for 4xx and 5xx status codes
response.raise_for_status()
data = response.json()
Service Type | Header Pattern | Example |
---|---|---|
API Key | X-API-Key |
{"X-API-Key": "abc123"} |
Bearer Token | Authorization |
{"Authorization": "Bearer token123"} |
Basic Auth | Authorization |
{"Authorization": "Basic dXNlcjpwYXNz"} |
Custom | Service-specific | {"X-RapidAPI-Key": "key123"} |
Many APIs require a User-Agent header:
Essential Patterns
.env
filesresponse.raise_for_status()
headers
dictionary for tokensRemember: These slides are your reference toolkit. Bookmark them! 🔖
LSE Summer School 2025 | ME204 Week 02 Day 03