pythongmailgoogle-api-clientgoogle-directory-api

googleapiclient.errors.HttpError: HttpError 404


I have imported google library

from googleapiclient.errors import HttpError, Error

and this is my code

response = service.users().get(userKey=email).execute()

user_id=(response['id'])
try:
    if user_id:
        return True
# except googleapiclient.errors.HttpError as e:
except googleapiclient.errors.HttpError as e:
    print(e)

but it is still not catching that exception.

Here is the error

googleapiclient.errors.HttpError: <HttpError 404 when requesting https://admin.googleapis.com/admin/directory/v1/users/cccc%404domain`enter code here`.com?alt=json returned "Resource Not Found: userKey". Details: "Resource Not Found: userKey">

purpose of my code is to check if that email already exists. If email exists it prints the email address but if email dose not exist it gives this error message above


Solution

  • I had the exact same problem today.

    Found the issue to be that the error is raising where you have .execute currently.

    Try this library

    from googleapiclient import errors
    

    Try putting execute into a while or if statement

    response = service.users().get(userKey=email)
    
    if response is not None:
        try:
            r = response.execute()
            user_email = r.get('primaryEmail', [])
            print(user_email)
    
        except errors.HttpError:
            print('Not found')