Python Forum
extract email addresses from gmail
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
extract email addresses from gmail
#1
hi
i just want to print all the email addresses present in my gmail contacts and have no idea
about how to do it.These email addresses are stored in google contacts(other) label.

I was able to print all my contact names from google people API but i don't know how to retrieve the email address.


Here is the code for retrieving names:
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/contacts.readonly']

def main():
    """Shows basic usage of the People API.
    Prints the name of the first 10 connections.
    """
    creds = None
    # The file token.pickle stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    service = build('people', 'v1', credentials=creds)

    # Call the People API
    print('List 10 connection names')
    results = service.people().connections().list(
        resourceName='people/me',
        pageSize=30,
        personFields='names,emailAddresses').execute()
    connections = results.get('connections', [])

    for person in connections:
        names = person.get('names', [])
        if names:
            name = names[0].get('displayName')
            print(name)

if __name__ == '__main__':
    main()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Extract PDF Attachment from Gmail jstaffon 0 581 Sep-10-2023, 01:55 PM
Last Post: jstaffon
  a function to get IP addresses of interfaces Skaperen 2 1,426 May-30-2022, 05:00 PM
Last Post: Skaperen
  Loop through list of ip-addresses [SOLVED] AlphaInc 7 3,967 May-11-2022, 02:23 PM
Last Post: menator01
  Gmail Sent email LIMIT Johnse 5 7,114 Apr-28-2021, 10:19 PM
Last Post: snippsat
  instance methods sharing addresses mim 1 2,243 Mar-28-2021, 05:22 AM
Last Post: deanhystad
  Convert email addresses to VCF format jehoshua 2 4,683 Mar-06-2021, 12:50 AM
Last Post: jehoshua
  crunching ip addresses snichols 5 2,804 Nov-10-2020, 05:24 PM
Last Post: snichols
  extract zip from .msg files and saving according to date stamp of email natjo 3 2,940 Aug-13-2020, 09:35 PM
Last Post: bowlofred
  Extract email addresses from string and store them in a list oslosurfer 2 2,705 Nov-24-2019, 03:35 PM
Last Post: oslosurfer
  how to upload a file to my gmail driver after login ?? evilcode1 5 4,158 Nov-06-2018, 07:33 AM
Last Post: evilcode1

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020