Hi,
ich habe heute noch eine neue Anwendung erstellt (Desktop) und somit eine neue secret.json bekommen, davor hab ich aber auch
https://www.googleapis.com/auth/photoslibrary zugelassen.
doch bekomme immer noch:
H:\>py notionFavorites.py
Token scopes: ['https://www.googleapis.com/auth/photoslibrary']
Status: 403
Antwort: {
"error": {
"code": 403,
"message": "Request had insufficient authentication scopes.",
"status": "PERMISSION_DENIED"
}
}
❌ Fehler: {
"error": {
"code": 403,
"message": "Request had insufficient authentication scopes.",
"status": "PERMISSION_DENIED"
}
}
Gefundene Favoriten: 0
Meine createToken Datei sieht so aus und funktioniert wunderbar:
import pickle
from google_auth_oauthlib.flow import InstalledAppFlow
# --- KONFIG ---
SCOPES = ['https://www.googleapis.com/auth/photoslibrary']
CREDENTIALS_FILE = r"H:\client_secret1.json" # Pfad zu deiner client_secret.json
TOKEN_FILE = r"H:\token1.pickle" # Hier wird das Token gespeichert
def main():
flow = InstalledAppFlow.from_client_secrets_file(CREDENTIALS_FILE, SCOPES)
try:
# Erst versuchen: Lokaler Webserver (Browser öffnet sich automatisch)
creds = flow.run_local_server(port=8080)
print("✅ Token über run_local_server erstellt.")
except Exception as e:
print("⚠️ run_local_server fehlgeschlagen:", e)
print("👉 Fallback: run_console (manueller Code-Eingabe)")
creds = flow.run_console()
# Token speichern
with open(TOKEN_FILE, "wb") as token:
pickle.dump(creds, token)
print(f"✅ Token gespeichert unter: {TOKEN_FILE}")
if __name__ == "__main__":
main()
Meine andere Datei liefert aber den obigen Fehler:
from __future__ import print_function
import os
import pickle
import requests
from google.auth.transport.requests import Request
# ------------------ KONFIGURATION ------------------
NOTION_TOKEN = "xxx" # Dein Notion-API-Token
DATABASE_ID = "xxx" # Deine Notion Datenbank-ID
CREDENTIALS_FILE = r"H:\client_secret1.json" # Von Google Cloud heruntergeladen
TOKEN_FILE = r"H:\token1.pickle"
SCOPES = ['https://www.googleapis.com/auth/photoslibrary']
# ------------------ GOOGLE PHOTOS ------------------
def get_google_creds():
creds = None
if os.path.exists(TOKEN_FILE):
with open(TOKEN_FILE, 'rb') as token:
creds = pickle.load(token)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
raise RuntimeError("❌ Kein gültiger Token. Bitte token.pickle erzeugen!")
return creds
def get_favorites(creds):
url = "https://photoslibrary.googleapis.com/v1/mediaItems:search"
headers = {
"Authorization": "Bearer " + creds.token,
"Content-Type": "application/json"
}
body = {
"filters": {
"featureFilter": {
"includedFeatures": ["FAVORITES"]
}
}
}
response = requests.post(url, headers=headers, json=body)
if response.status_code == 200:
results = response.json()
return results.get('mediaItems', [])
else:
print(f"❌ Fehler: {response.text}")
return []
def test_photos_api(creds):
import requests
url = "https://photoslibrary.googleapis.com/v1/mediaItems"
headers = {
"Authorization": "Bearer " + creds.token,
"Content-Type": "application/json"
}
r = requests.get(url, headers=headers)
print("Status:", r.status_code)
print("Antwort:", r.text)
# ------------------ NOTION ------------------
def upload_to_notion(photo):
url = "https://api.notion.com/v1/pages"
headers = {
"Authorization": f"Bearer {NOTION_TOKEN}",
"Content-Type": "application/json",
"Notion-Version": "2022-06-28"
}
data = {
"parent": {"database_id": DATABASE_ID},
"properties": {
"Bildname": {"title": [{"text": {"content": photo['filename']}}]},
"Id": {"rich_text": [{"text": {"content": photo['id']}}]},
"Datum": {"date": {"start": photo.get('mediaMetadata', {}).get('creationTime', None)}},
"Fotos": {
"files": [
{
"name": photo['filename'],
"external": {"url": photo['baseUrl'] + "=w2048-h1024"}
}
]
}
}
}
r = requests.post(url, headers=headers, json=data)
if r.status_code == 201:
print(f"✅ {photo['filename']} in Notion eingetragen")
else:
print(f"❌ Fehler bei {photo['filename']}: {r.text}")
# ------------------ MAIN ------------------
def main():
creds = get_google_creds()
print("Token scopes:", creds.scopes)
test_photos_api(creds)
favorites = get_favorites(creds)
print(f"Gefundene Favoriten: {len(favorites)}")
for photo in favorites:
upload_to_notion(photo)
if __name__ == '__main__':
main()
Hab ich da vielleicht eine fehler drin?
Danke für die Hilfe
Gruß Rose