google-calendar-apigoogle-authenticationgoogle-developers-consolegoogle-client

how to get email address along with credential for calendar after user allow permission


how to get owner email address after user allow permission

  SCOPES = ['https://www.googleapis.com/auth/calendar']  
  flow = InstalledAppFlow.from_client_secrets_file(credentials.json, SCOPES)
  creds = flow.run_local_server(port=0)
  service = build('calendar', 'v3', credentials=creds)
  event_body = {
     'summary': 'Test calendar',
    'location': 'Gurgaon, Haryana',
    'description': 'A chance to hear more about Google\'s developer 
      products.',
  'start': {
    'dateTime': '2020-03-28T09:00:00-07:00',
    'timeZone': 'Asia/Kolkata',
    },
'end': {
    'dateTime': '2020-03-28T17:00:00-07:00',
    'timeZone': 'America/Los_Angeles',
   },

'attendees': [
    {'email': 'user@gmail.com'},
   ],
'reminders': {
    'useDefault': False,
  },
"visibility": "public",
   }
 event = service.events().insert(calendarId='primary',body=evnt_body)  

Also tried to add 'https://www.googleapis.com/auth/userinfo.email' this scope but it returns None in credential. My need to add owner user's email address in attendances list


Solution

  • You can make a call to Users.getProfile, and provide the special value me to userId to indicate the authenticated user:

    gmail_service = build('gmail', 'v1', credentials=creds)
    email_address = gmail_service.users().getProfile(userId='me').execute()["emailAddress"]
    

    Note:

    Reference: