Rufe die YouTube Data API v3 mit curl + jq auf. Das OAuth-Bearer-Token des Benutzers befindet sich in $GOOGLE_YOUTUBE_TOKEN; jeder Aufruf benötigt es als Authorization: Bearer $GOOGLE_YOUTUBE_TOKEN. Basis-URL:
https://www.googleapis.com/youtube/v3.
Das Token trägt immer youtube.readonly plus Identitäts-Scopes (openid email profile); wenn der Benutzer es bei der Installation aktiviert hat, trägt es auch youtube.upload (zum Veröffentlichen von Videos).
Antworten sind Standard-JSON; Fehler erscheinen als {"error": {"code": 401|403|..., "message": "..."}} – zeige diesen Fehler wortwörtlich an. 401 → Token abgelaufen, der Benutzer muss den YouTube-Connector neu verbinden. 403 insufficientPermissions bei einem Upload → der Benutzer hat youtube.upload nicht gewährt; bitte ihn, den Connector mit aktiviertem Upload-Kästchen neu zu verbinden.
Beginne immer mit der Kanal-Überprüfung, um zu bestätigen, dass die Verbindung funktioniert und zu erfahren, gegen welchen Kanal du arbeitest.
curl -sS -H "Authorization: Bearer $GOOGLE_YOUTUBE_TOKEN" \
"https://www.googleapis.com/youtube/v3/channels?part=snippet,statistics,contentDetails&mine=true" \
| jq '.items[0] | {title: .snippet.title, subs: .statistics.subscriberCount, views: .statistics.viewCount, uploads: .contentDetails.relatedPlaylists.uploads}'
YouTube durchsuchen
# Öffentliche Suche (beliebiges Video). type kann video|channel|playlist sein.
curl -sS -H "Authorization: Bearer $GOOGLE_YOUTUBE_TOKEN" \
--data-urlencode "q=AI Video Automatisierung" \
--data-urlencode "part=snippet" \
--data-urlencode "type=video" \
--data-urlencode "maxResults=10" \
-G "https://www.googleapis.com/youtube/v3/search" \
| jq '.items[] | {videoId: .id.videoId, title: .snippet.title, channel: .snippet.channelTitle, published: .snippet.publishedAt}'
Füge --data-urlencode "order=date|viewCount|rating|relevance" zum Sortieren hinzu, oder --data-urlencode "publishedAfter=2026-01-01T00:00:00Z" zum Eingrenzen.
Meine hochgeladenen Videos ansehen
YouTube hat keinen direkten Aufruf "meine Videos auflisten" – lies die Uploads-Playlist des Kanals und blättere dann durch deren Elemente.
# 1. Hole die ID der Uploads-Playlist (UU... ) – wie im Kanal-Aufruf oben.
UPLOADS=$(curl -sS -H "Authorization: Bearer $GOOGLE_YOUTUBE_TOKEN" \
"https://www.googleapis.com/youtube/v3/channels?part=contentDetails&mine=true" \
| jq -r '.items[0].contentDetails.relatedPlaylists.uploads')
# 2. Liste die letzten Uploads auf (50/Seite; folge .nextPageToken für mehr).
curl -sS -H "Authorization: Bearer $GOOGLE_YOUTUBE_TOKEN" \
-G "https://www.googleapis.com/youtube/v3/playlistItems" \
--data-urlencode "part=snippet,contentDetails" \
--data-urlencode "playlistId=$UPLOADS" \
--data-urlencode "maxResults=50" \
| jq '.items[] | {videoId: .contentDetails.videoId, title: .snippet.title, published: .snippet.publishedAt}'
Blättere, indem du --data-urlencode "pageToken=$PAGE_TOKEN" mit dem .nextPageToken aus der vorherigen Antwort übergibst.
Video-Statistiken (Aufrufe / Likes / Kommentare)
# Akzeptiert eine kommagetrennte ID-Liste.
curl -sS -H "Authorization: Bearer $GOOGLE_YOUTUBE_TOKEN" \
-G "https://www.googleapis.com/youtube/v3/videos" \
--data-urlencode "part=snippet,statistics,status" \
--data-urlencode "id=VIDEO_ID_1,VIDEO_ID_2" \
| jq '.items[] | {title: .snippet.title, views: .statistics.viewCount, likes: .statistics.likeCount, comments: .statistics.commentCount, privacy: .status.privacyStatus}'
Kommentare zu einem Video lesen
curl -sS -H "Authorization: Bearer $GOOGLE_YOUTUBE_TOKEN" \
-G "https://www.googleapis.com/youtube/v3/commentThreads" \
--data-urlencode "part=snippet" \
--data-urlencode "videoId=VIDEO_ID" \
--data-urlencode "maxResults=20" \
--data-urlencode "order=relevance" \
| jq '.items[] | .snippet.topLevelComment.snippet | {author: .authorDisplayName, text: .textDisplay, likes: .likeCount}'
Ein Video hochladen (benötigt youtube.upload)
Bestätige mit dem Benutzer vor dem Veröffentlichen – zeige den Titel, die Privatsphäre-Einstellung und die Datei an, die du hochladen möchtest. Uploads sind ein zweistufiger wiederaufnehmbarer Ablauf: Initialisierung mit Metadaten → PUT der Bytes.
FILE="/pfad/zum/video.mp4"
TITLE="Mein Titel"
DESC="Meine Beschreibung"
# privacyStatus: public | unlisted | private
read -r -d '' META <<JSON
{"snippet":{"title":"$TITLE","description":"$DESC","categoryId":"22"},
"status":{"privacyStatus":"unlisted","selfDeclaredMadeForKids":false}}
JSON
# 1. Wiederaufnehmbare Sitzung initialisieren -> Upload-URL aus dem Location-Header erfassen.
UPLOAD_URL=$(curl -sS -D - -o /dev/null \
-H "Authorization: Bearer $GOOGLE_YOUTUBE_TOKEN" \
-H "Content-Type: application/json; charset=UTF-8" \
-H "X-Upload-Content-Type: video/*" \
-X POST "https://www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet,status" \
-d "$META" | tr -d '\r' | awk '/^[Ll]ocation:/{print $2}')
# 2. Bytes hochladen -> gibt die erstellte Video-Ressource zurück (hat .id).
curl -sS -H "Authorization: Bearer $GOOGLE_YOUTUBE_TOKEN" \
-H "Content-Type: video/*" \
-X PUT --upload-file "$FILE" "$UPLOAD_URL" \
| jq '{id: .id, url: ("https://www.youtube.com/watch?v=" + .id), privacy: .status.privacyStatus}'
categoryId 22 = "Menschen & Blogs" (eine sichere Standardeinstellung). Um ein benutzerdefiniertes Thumbnail festzulegen (die Datei muss zuerst verarbeitet werden), rufe POST /upload/youtube/v3/thumbnails/set?videoId=VIDEO_ID mit dem Bild auf.
Fallstricke
- Kontingent: Die Data API hat ein Kontingent (standardmäßig 10.000 Einheiten/Tag). Eine
searchkostet 100 Einheiten; einuploadkostet ~1.600. Eine Häufung von Suchanfragen kann das tägliche Kontingent erschöpfen →403 quotaExceeded; zeige es deutlich an. - Kein "Meine Videos"-Endpunkt – gehe immer über die Uploads-Playlist.
search-Ergebnisse sind letztendlich konsistent – ein frisch hochgeladenes Video erscheint möglicherweise erst nach Minuten/Stunden insearch; lies es stattdessen über die Uploads-Playlist oder per ID.- Hochgeladene Videos starten im Zustand
uploaded/processing; Statistiken sind0, bis die Verarbeitung abgeschlossen ist.