0

i am trying to read my Google Calender events from a specific Calendar without a reauthentication after X hours/days for a dashboards. Is that possible?

I added the "Google Calendar API" to my Google Cloud Platform-project, created a "Desktop client" (with client and secret) and can access my dates via the python api:

SCOPES = [
    'https://www.googleapis.com/auth/calendar.readonly', "openid",
    "https://www.googleapis.com/auth/userinfo.email",
    "https://www.googleapis.com/auth/userinfo.profile"
    ]


client_config = {
    'installed': {
        'client_id': '123456.apps.googleusercontent.com',
        'project_id': 'test-dashboard',
        'auth_uri': 'https://accounts.google.com/o/oauth2/auth',
        'token_uri': 'https://oauth2.googleapis.com/token',
        'auth_provider_x509_cert_url': 'https://www.googleapis.com/oauth2/v1/certs',
        'client_secret': 'secret',
        'redirect_uris': ['urn:ietf:wg:oauth:2.0:oob', 'http://localhost']
        }
    }

flow = InstalledAppFlow.from_client_config(client_config, SCOPES)
credentials = flow.run_local_server()
service =  build('calendar', 'v3', credentials = credentials)

page_token = None
cal = {}
while True:
  calendar_list = service.calendarList().list(pageToken=page_token).execute()
  for calendar_list_entry in calendar_list['items']:
    cal[calendar_list_entry['summary']] = calendar_list_entry
  page_token = calendar_list.get('nextPageToken')
  if not page_token:
    break

But the problem is, that flow.run_local_server() ask everytime for my Google Account, which is not possible for a 24/7 running dashboard.

Kind regards

Edit: I am using a standard gmail-account within gcp. I tried it with a service account, but the result, if i use this method, is empty and not the events from my gmail account. Is it possible to get the calenders events from my personal account via the service account?

If i am using domain-wide delegation, i could request the calendar events from the domain, but that would not help me to receive the events from the personal account, does it?

viertel97
  • 123
  • 1
  • 8
  • When you authorize the first time, request **offline** access. This will then provide you with a Refresh Token that can be used to refresh the Access Token. However, that will continue to work for about a week before an authorization is required again. There are other options such as using a service account and impersonating a user via Domain Wide Delegation. Your question does not have details on the type of user account (Gmail or Workspace). These tips will help you research this further. – John Hanley Nov 02 '21 at 19:42
  • I edited the questions above. – viertel97 Nov 03 '21 at 06:47

1 Answers1

0

I got it working: I invited the service user to one of the private calenders and inserted it afterwards. Now i can read the events from the private account with my service account.

viertel97
  • 123
  • 1
  • 8