Чтобы получить дни рождения, сохраненные в Контактах Google, вы можете использовать API Google People. Вот пример кода на Python:
import google.auth
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
# Load credentials from token.json (generated after authenticating with Google)
creds = None
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json')
# If credentials are not found or are expired, authenticate and save the token
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = google.auth.default()
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.json', 'w') as token:
token.write(creds.to_json())
# Build the People API service
service = build('people', 'v1', credentials=creds)
# Request the list of contacts
results = service.people().connections().list(
resourceName='people/me',
pageSize=1000,
personFields='birthdays'
).execute()
# Iterate over the contacts and extract birthdays
birthdays = []
connections = results.get('connections', [])
for person in connections:
birthdays.extend(person.get('birthdays', []))
# Print the birthdays
for birthday in birthdays:
date = birthday['date']
print(f"Name: {birthday['person']['names'][0]['displayName']}")
print(f"Birthday: {date['year']}-{date['month']}-{date['day']}")
Этот код проверяет подлинность приложения с помощью API Google People, получает список контактов и извлекает дни рождения для каждого контакта.
Обратите внимание, что для использования этого кода вам необходимо настроить Google People API и получить учетные данные (идентификатор клиента и секретный код клиента). Вам также необходимо установить необходимые пакеты Python: google-auth, google-auth-oauthlib, google-auth-httplib2и . >google-api-python-client.