Search code examples
pythongoogle-apigoogle-people-api

Google People API cannot find other users profile pictures


I have been working on an email signature generator for my company to generate email signatures via the google API for everyone in our company. I have all of my data collection except getting user profile pictures ( we want these in the signatures). Curiously, the email address I set as the subject in the credentials returns the profile picture (my email), but none of the other accounts do.

Here is the code that I am testing with to get just the photos to return, everything else is working in a separate file already.

It is a bit messy right now since I am just trying to get something to return:

SERVICE_ACCOUNT_FILE = 'creds.json'
src = 'DIRECTORY_SOURCE_TYPE_DOMAIN_PROFILE'
credentials = service_account.Credentials.from_service_account_file(
    filename=SERVICE_ACCOUNT_FILE,
    scopes=['https://www.googleapis.com/auth/gmail.settings.basic',
            'https://www.googleapis.com/auth/gmail.settings.sharing',
            'https://www.googleapis.com/auth/userinfo.profile',
            'https://www.googleapis.com/auth/directory.readonly'
            ],
    subject = 'mygoogleacount@domainname.com'
)
service = build('people', 'v1', credentials=credentials)



results = service.people().listDirectoryPeople(
    readMask='names,photos',
    sources=src,
    pageSize=1000
).execute()

directory_people = results.get('people', [])

# NEXT PAGE TOKEN
next_page_token = results.get('nextPageToken')

for i, person in enumerate(directory_people):
    name = person.get('names', [])
    photo = person.get('photos', [])
    
print(name + photo)

while next_page_token:
    results = service.people().listDirectoryPeople(
        readMask='names,emailAddresses,phoneNumbers,organizations,photos',
        sources=src,
        pageSize=1000,
        pageToken=next_page_token
    ).execute()

    directory_people = results.get('people', [])
    next_page_token = results.get('nextPageToken')
    print(next_page_token)

    for i, person in enumerate(directory_people):
        name = person.get('names', [])
        photo = person.get('photos', [])
       
        print(name + photo)

If I just print out the photos, rather than name and photo, the output looks like this:

[]
[]
[]
[]
[]
[]
[]
[link to my subject email accounts profile pic]
[]
[]
[]
[]

Does anyone have any idea why I cannot return anyone else's profile picture? Profile pictures are visible to everyone in our domain, and my service account has domain wide delegation. I would appreciate a fresh set of eyes on this, appreciate any insight!


Solution

  • This is probably related to your delegation.

    When you are using the service account you are delignating as a user subject = 'mygoogleacount@domainname.com' For all intensive purposes the service ac count is now pretending to be that user. so the only user who's photo you are going to see is 'mygoogleacount@domainname.com' if you want to see someone else's photo your going to have to delegate to that user.