def main(): """ main function """ global email # space separated list of authorized API scopes for this service account scope = 'https://apps-apis.google.com/a/feeds/emailsettings/2.0/' # create our email settings client client = EmailSettingsClient(domain=email.split('@')[1]) # obtain our street cred credentials = get_cred(email, scope) # fetch our access token auth2token = OAuth2TokenFromCredentials(credentials) # authorize our client auth2token.authorize(client) with open(phile, 'rb') as f: reader = csv.reader(f, delimiter=',', quoting=csv.QUOTE_NONE) for r in reader: try: if '@' in r[0]: forwarding = client.RetrieveForwarding( username=r[0].split('@')[0] ).property[1].value if forwarding: print("{}|{}".format(r[0], forwarding)) except: print("{}|Error".format(r[0]))
def search(request): forwarding = None emails = None if request.method == "POST": form = SearchForm(request.POST) if form.is_valid(): cd = form.cleaned_data # space separated list of authorized API scopes for # the service account scope = 'https://apps-apis.google.com/a/feeds/emailsettings/2.0/' # create our email settings client client = EmailSettingsClient( domain=settings.DOMAIN_SUPER_USER_EMAIL.split('@')[1]) # obtain our street cred credentials = get_cred(settings.DOMAIN_SUPER_USER_EMAIL, scope) # fetch our access token auth2token = OAuth2TokenFromCredentials(credentials) # authorize our client auth2token.authorize(client) if cd['username']: try: forwarding = client.RetrieveForwarding( username=cd["username"]).property[1].value except: forwarding = None elif request.FILES.get('phile'): save_path = os.path.join(settings.MEDIA_ROOT, 'files/emailsettings.csv') path = default_storage.save(save_path, request.FILES['phile']) emails = [] with open(path, 'rb') as f: reader = csv.reader(f, delimiter=',', quoting=csv.QUOTE_NONE) for r in reader: try: if '@' in r[0]: forwarding = client.RetrieveForwarding( username=r[0].split( '@')[0]).property[1].value if forwarding: emails.append(forwarding) except: forwarding = None else: forwarding = 'Error' else: form = SearchForm() return render(request, 'emailsettings/search.html', { 'form': form, 'forwarding': forwarding, 'emails': emails })
def __init__(self, domain, email, password, app): """Constructor for the EmailSettingsSample object. Takes an email, password and an app id corresponding to a google apps admin account to demo the Email Settings API. Args: domain: [string] The domain name (e.g. domain.com) email: [string] The e-mail address of a domain admin account. password: [string] The domain admin's password. app: [string] The app name of the form companyName-applicationName-versionID """ self.client = EmailSettingsClient(domain=domain) self.client.ClientLogin(email=email, password=password, source=app)
def main(): """ main function """ global email # space separated list of authorized API scopes for this service account scope = 'https://apps-apis.google.com/a/feeds/emailsettings/2.0/' # create our email settings client client = EmailSettingsClient(domain=email.split('@')[1]) # obtain our street cred credentials = get_cred(email, scope) # fetch our access token auth2token = OAuth2TokenFromCredentials(credentials) # authorize our client auth2token.authorize(client) forwarding = client.RetrieveForwarding(username=username) #print forwarding #print forwarding.__dict__ print forwarding.property[1].value
def google_get_emailsettings_credentials(): ''' Google's EmailSettings API is not yet service-based, so delegation data has to be accessed differently from our other Google functions. TODO: Refactor when API is updated. ''' client = EmailSettingsClient(domain=settings.GOOGLE_DOMAIN) credentials = ServiceAccountCredentials.from_json_keyfile_name( settings.GOOGLE_PATH_TO_KEYFILE, scopes='https://apps-apis.google.com/a/feeds/emailsettings/2.0/' ).create_delegated(settings.GOOGLE_SUB_USER) auth2token = gdata.gauth.OAuth2TokenFromCredentials(credentials) auth2token.authorize(client) return client
class EmailSettingsSample(object): """EmailsSettingsSample object demos the Email Settings API.""" def __init__(self, domain, email, password, app): """Constructor for the EmailSettingsSample object. Takes an email, password and an app id corresponding to a google apps admin account to demo the Email Settings API. Args: domain: [string] The domain name (e.g. domain.com) email: [string] The e-mail address of a domain admin account. password: [string] The domain admin's password. app: [string] The app name of the form companyName-applicationName-versionID """ self.client = EmailSettingsClient(domain=domain) self.client.ClientLogin(email=email, password=password, source=app) def run(self, username, setting, method, args): """Method that invokes the EmailSettingsClient services Args: username: [string] The name of the account for whom to get/set settings setting: [string] The email setting to be got/set/updated method: [string] Specifies the get or set method """ if setting == 'label': if method == 'get': print "getting labels for %s...\n" % (username) print self.client.RetrieveLabels(username=username) elif method == 'set': print "creating label for %s...\n" % (username) print self.client.CreateLabel(username=username, name=LABEL_NAME) else: print "deleting labels isn't supported" elif setting == 'forwarding': if method == 'get': print "getting forwarding for %s...\n" % (username) print self.client.RetrieveForwarding(username) elif method == 'set': print "updating forwarding settings for %s...\n" % (username) print self.client.UpdateForwarding( username=username, enable=not (options.disable), forward_to=FORWARD_TO, action=FORWARDING_ACTION) else: print "deleting forwarding settings isn't supported" elif setting == 'sendas': if method == 'get': print "getting sendAs alias for %s...\n" % (username) print self.client.RetrieveSendAs(username=username) elif method == 'set': print "creating sendAs alias for %s...\n" % (username) print self.client.CreateSendAs( username=username, name=SEND_AS_NAME, address=SEND_AS_ADDRESS, reply_to=SEND_AS_REPLY_TO, make_default=SEND_AS_MAKE_DEFAULT) else: print "deleting send-as settings isn't supported" elif setting == 'pop': if method == 'get': print "getting pop settings for %s...\n" % (username) print self.client.RetrievePop(username=username) elif method == 'set': print "updating pop settings for %s...\n" % (username) print self.client.UpdatePop(username=username, enable=not (options.disable), enable_for=POP_ENABLE_FOR, action=POP_ACTION) else: print "deleting pop settings isn't supported" elif setting == 'signature': if method == 'get': print "getting signature for %s...\n" % (username) print self.client.RetrieveSignature(username=username) elif method == 'set': print "updating signature for %s...\n" % (username) print self.client.UpdateSignature(username=username, signature=SIGNATURE) else: print "deleting signature settings isn't supported" elif setting == 'vacation': if method == 'get': print "getting vacation settings for %s...\n" % (username) print self.client.RetrieveVacation(username=username) elif method == 'set': print "updating vacation settings for %s...\n" % (username) print self.client.UpdateVacation( username=username, enable=not (options.disable), subject=VACATION_SUBJECT, message=VACATION_MESSAGE, contacts_only=VACATION_CONTACTS_ONLY) else: print "deleting vacation settings isn't supported" elif setting == 'imap': if method == 'get': print "getting imap settings for %s...\n" % (username) print self.client.RetrieveImap(username) elif setting == 'set': print "updating imap settings for %s...\n" % (username) print self.client.UpdateImap(username=username, enable=not (options.disable)) else: print "deleting imap settings isn't supported" elif setting == 'filter': if method == 'get': print "getting email filters is not yet possible\n" parser.print_help() elif method == 'set': print "creating an email filter for %s...\n" % (username) print self.client.CreateFilter( username=username, from_address=FILTER_FROM, to_address=FILTER_TO, subject=FILTER_SUBJECT, has_the_word=FILTER_HAS_THE_WORD, does_not_have_the_word=FILTER_DOES_NOT_HAVE_THE_WORD, has_attachments=FILTER_HAS_ATTACHMENT, label=FILTER_LABEL, mark_as_read=FILTER_SHOULD_MARK_AS_READ, archive=FILTER_SHOULD_ARCHIVE) else: print "deleting filters isn't supported" elif setting == 'general': if method == 'get': print "getting general email settings is not yet possible\n" parser.print_help() elif method == 'set': print "updating general settings for %s...\n" % (username) print self.client.UpdateGeneralSettings( username=username, page_size=GENERAL_PAGE_SIZE, shortcuts=GENERAL_ENABLE_SHORTCUTS, arrows=GENERAL_ENABLE_ARROWS, snippets=GENERAL_ENABLE_SNIPPETS, use_unicode=GENERAL_ENABLE_UNICODE) else: print "deleting general settings isn't supported" elif setting == 'language': if method == 'get': print "getting language settings is not yet possible\n" parser.print_help() elif method == 'set': print "updating language for %s...\n" % (username) print self.client.UpdateLanguage(username=username, language=LANGUAGE) else: print "deleting language settings isn't supported" elif setting == 'webclip': if method == 'get': print "getting webclip settings is not yet possible\n" parser.print_help() elif method == 'get': print "updating webclip settings for %s...\n" % (username) print self.client.UpdateWebclip(username=username, enable=not (options.disable)) else: print "deleting webclip settings isn't supported" elif setting == 'delegation': if method == 'get': print "getting email delegates for %s..." % (username) print self.client.RetrieveEmailDelegates(username=username) elif method == 'set': address = args['delegationId'] print "adding %s as an email delegate to %s..." % (address, username) print self.client.AddEmailDelegate(username=username, address=address) else: address = args['delegationId'] print "deleting %s as an email delegate for %s..." % (address, username) print self.client.DeleteEmailDelegate(username=username, address=address) else: parser.print_help()
def main(): """ main function """ global username global email global client if username: scope = 'https://apps-apis.google.com/a/feeds/emailsettings/2.0/' credentials = get_cred(email, scope) auth2token = OAuth2TokenFromCredentials(credentials) # create our email settings client client = EmailSettingsClient(domain=email.split('@')[1]) auth2token.authorize(client) forwarding = client.RetrieveForwarding(username=username) #print forwarding #print forwarding.__dict__ print forwarding.property[1].value else: # storage for credentials storage = Storage(settings.STORAGE_FILE) # create an http client http = httplib2.Http() # email settings scope scope = 'https://apps-apis.google.com/a/feeds/emailsettings/2.0/' # obtain the admin directory user cred users_cred = storage.get() if users_cred is None or users_cred.invalid: users_cred = get_cred(email, "admin.directory.user") storage.put(users_cred) else: users_cred.refresh(http) # build the service connection service = build("admin", "directory_v1", http=users_cred.authorize(http)) # fetch all of our users and put the results into a list user_list = [] # intialize page_token page_token = None while True: results = service.users().list(domain=email.split('@')[1], maxResults=500, pageToken=page_token, orderBy='familyName', viewType='domain_public').execute() page_token = results.get('nextPageToken') user_list.append(results) if not page_token: break print "length of user_list: {}".format(len(user_list)) # we cycle through all of the users and fetch their # email settings, looking for the forwardTo key for users in user_list: for user in users["users"]: pmail = user["primaryEmail"] username = pmail.split('@')[0] given_name = user['name']['givenName'] family_name = user['name']['familyName'] # create our email settings client client = EmailSettingsClient(domain=email.split('@')[1]) # obtain street cred credentials = get_cred(email, scope) auth2token = OAuth2TokenFromCredentials(credentials) auth2token.authorize(client) try: forwarding = client.RetrieveForwarding(username=username) if forwarding.property[1].value: print "{}|{}|{}|{}".format( family_name, given_name, pmail, forwarding.property[1].value) except Exception, e: logger.debug("{}|{}|{}|{}".format(family_name, given_name, pmail, str(e)))