Доступ к API Google Фото с помощью Python с использованием google-api-python-client

Чтобы получить доступ к API Google Photos с помощью Python и библиотеки google-api-python-client, вы можете выполнить следующие действия:

  1. Установите необходимые библиотеки:

    pip install google-api-python-client google-auth google-auth-oauthlib google-auth-httplib2
  2. Настройте проект Google Cloud:

    • Перейти к Облачная консоль Google .
    • Создайте новый проект или выберите существующий.
    • Включите API Google Фото для своего проекта.
    • Создайте учетные данные API (идентификатор клиента OAuth 2.0) для своего проекта.
  3. Создайте скрипт Python и импортируйте необходимые модули:

    from googleapiclient.discovery import build
    from google_auth_oauthlib.flow import InstalledAppFlow
    from google.auth.transport.requests import Request
  4. Аутентификация и авторизация клиента API:

    # Set up the OAuth 2.0 client flow
    flow = InstalledAppFlow.from_client_secrets_file(
       'path/to/client_secret.json',
       scopes=['https://www.googleapis.com/auth/photoslibrary'])
    # If there are no valid credentials available, let the user log in.
    if not credentials or not credentials.valid:
       if credentials and credentials.expired and credentials.refresh_token:
           credentials.refresh(Request())
       else:
           credentials = flow.run_local_server(port=0)
    # Build the Google Photos API client
    service = build('photoslibrary', 'v1', credentials=credentials)
  5. Отправить запросы к API:

    # Example: List all albums
    response = service.albums().list().execute()
    albums = response.get('albums', [])
    # Iterate through the albums and print their titles
    for album in albums:
       print(album['title'])

Обратите внимание, что файл client_secret.json, упомянутый на шаге 4, должен содержать ваши учетные данные клиента OAuth 2.0, полученные из Google Cloud Console.