Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Legg til støtte for automatisk generering av events ved hjelp av Google calendar API og AI (openai/open assistant) #130

Open
AndreasJJ opened this issue Mar 24, 2023 · 0 comments

Comments

@AndreasJJ
Copy link
Collaborator

AndreasJJ commented Mar 24, 2023


from google.oauth2.credentials import Credentials
from google.oauth2 import id_token
from google.auth.transport import requests
from googleapiclient.discovery import build

# Obtain the ID token from the client-side authentication flow
id_token = 'your_id_token_here'

# Verify the ID token
client_id = 'your_client_id_here'
try:
    idinfo = id_token.verify_oauth2_token(id_token, requests.Request(), client_id)
    if idinfo['iss'] not in ['accounts.google.com', 'https://accounts.google.com']:
        raise ValueError('Invalid issuer.')
    # Get the user's email address and user ID
    email = idinfo['email']
    user_id = idinfo['sub']
except ValueError:
    # Invalid token
    pass

# Build the credentials object using the access token
creds = Credentials.from_authorized_user_info(info=idinfo)

# Build the Calendar API client
service = build('calendar', 'v3', credentials=creds)

# Ask the user to select a calendar to access
calendar_list = service.calendarList().list().execute()
for i, calendar in enumerate(calendar_list['items']):
    print(f"{i + 1}. {calendar['summary']}")
calendar_index = int(input("Select a calendar to access: ")) - 1
calendar_id = calendar_list['items'][calendar_index]['id']

# Set the date range for the events you want to retrieve
start_date = datetime.datetime.utcnow().isoformat() + 'Z'
end_date = (datetime.datetime.utcnow() + datetime.timedelta(days=7)).isoformat() + 'Z'

# Retrieve events from the calendar
events_result = service.events().list(calendarId=calendar_id, timeMin=start_date, timeMax=end_date, singleEvents=True, orderBy='startTime').execute()
events = events_result.get('items', [])

# Print out the events
if not events:
    print('No events found for the specified date range.')
else:
    print('Upcoming events:')
    for event in events:
        start = event['start'].get('dateTime', event['start'].get('date'))
        print(f'{start} - {event["summary"]}')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant