Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Download JSON Data From An API With Query Parameters In Python

NOTE: This is using `requests`, it's probably better to use the explicit python library

Code

#!/usr/bin/env python3

import keyring
import json 
import requests

base_url = 'https://www.googleapis.com/youtube/v3/playlistss'

params = {
    'key': keyring.get_password('password_name', 'account_name'),
    'maxResults': 50,
    'part': 'id,contentDetails,snippet,status',
    'id': 'OLAK5uy_mlSFkDjSZamS7gRlBIgrXMNlNmrHyAxtk' 
}

response = requests.get(base_url, params=params)
if response.status_code == 200:
    json_data = response.json()
    json_string = json.dumps(json_data, sort_keys=True, indent=2, default=str)
    print(json_string)
else:
    print(f"Error with status code: {response.status_code}")