Exemple #1
0
 def create_contact_group(cls, credentials):
     auth_token = OAuth2TokenFromCredentials(credentials)
     gd_client = ContactsClient()
     auth_token.authorize(gd_client)
     new_group = gdata.contacts.data.GroupEntry(title=atom.data.Title(
         text='ioGrow Contacts'))
     created_group = gd_client.CreateGroup(new_group)
     return created_group.id.text
Exemple #2
0
 def list_google_contacts(cls, credentials):
     auth_token = OAuth2TokenFromCredentials(credentials)
     gd_client = ContactsClient()
     auth_token.authorize(gd_client)
     query = gdata.contacts.client.ContactsQuery()
     query.max_results = 10
     feed = gd_client.GetContacts(q=query)
     for i, entry in enumerate(feed.entry):
         if entry.name:
             print '\n%s %s' % (i + 1, smart_str(entry.name.full_name.text))
         try:
             contact_image = gd_client.GetPhoto(entry)
         except:
             print 'ERROR: not found'
Exemple #3
0
    def __get_client(self):
        '''Login to Google and return a ContactsClient object.

        '''
        if not self.__client:
            if not self.config.email or not self.password:
                print >> sys.stderr, "ERROR: Missing email or password"
                sys.exit(1)
            client = ContactsClient()
            client.ssl = True
            client.ClientLogin(email=self.config.email,
                               password=self.password,
                               service='cp',
                               source='goobook')
            self.__client = client
        return self.__client
def invite_google(request):

    all_emails = []
    if 'token' in request.GET:

        token = request.GET['token']
        token_auth_login = AuthSubToken(token)

        query = ContactsQuery()
        query.max_results = MAX_RESULT

        client = ContactsClient(auth_token=token_auth_login)
        client.upgrade_token(token=token_auth_login)

        try:
            feed = client.GetContacts(q=query)
            while feed:
                next = feed.GetNextLink()

                for entry in feed.entry:
                    try:
                        email_address = entry.email[0].address
                        #print email_address
                        all_emails.append(email_address)
                    except:
                        pass

                feed = None
                if PAGING and next:
                    feed = client.GetContacts(next.href,
                                              auth_token=token_auth_login,
                                              q=query)
        except:
            pass

    return render(request, 'pin/invite_google.html', {
        'login': GetAuthSubUrl(),
        'all_emails': all_emails
    })
Exemple #5
0
 def _contacts_client(self):
     return ContactsClient(source="SchedUp",
                           auth_token=ClientLoginToken(
                               self.oauth.credentials.access_token))
Exemple #6
0
 def create_contact(cls, credentials, google_contact_schema):
     auth_token = OAuth2TokenFromCredentials(credentials)
     gd_client = ContactsClient()
     auth_token.authorize(gd_client)
     contact_entry = gd_client.CreateContact(google_contact_schema)
     return contact_entry.id.text
Exemple #7
0
    def __init__(self, account):

        # noinspection PyUnresolvedReferences
        def delegate():
            """
            Function to access as another account using server-side credentials.
            :return: Boolean, True if successful.
            """
            try:
                self.dc = self.credentials.create_delegated(self.account)
                return True
            except Exception as e:
                logger.exception("Unexpected error when trying to get permission to access this account")
                return False

        # noinspection PyUnresolvedReferences
        def validate():
            """
            Function to validate your server-side credentials, generating an access token.
            :return: Boolean, True if successful.
            """
            is_ok = False
            self.credentials = ServiceAccountCredentials.from_json_keyfile_name(self.json, scopes=self.scopes)
            try:
                if not self.credentials:
                    logger.error('Problem when trying to validate your credentials.')
                elif self.credentials.invalid:
                    logger.error("Access denied, invalid credentials")
                else:
                    is_ok = True
                return is_ok
            except:
                logger.exception("Problem when trying to validate your credentials.")
                return is_ok

        self.credentials = None
        self.calendar_list = None
        self.dc = None
        # Load Google credentials
        self.json = 'credentials/google.json'

        # Load all the Application Scope used to handle Calendar and Contacts
        self.scopes = ['https://www.googleapis.com/auth/calendar',
                       'https://www.googleapis.com/auth/contacts',
                       'https://www.google.com/m8/feeds/',
                       'https://www.googleapis.com/auth/admin.directory.user',
                       'https://www.googleapis.com/auth/admin.directory.orgunit.readonly',
                       'https://www.googleapis.com/auth/admin.directory.orgunit',
                       'https://www.googleapis.com/auth/apps.groups.migration',
                       'https://www.googleapis.com/auth/gmail.insert',
                       'https://www.googleapis.com/auth/gmail.labels',
                       'https://mail.google.com/',
                       'https://www.googleapis.com/auth/gmail.modify']

        # Stores the User Account
        self.account = str(account)

        # ERROR CODE FOR LOGGING
        self.error_code = {'delegate': '[' + self.account + '][DELEGATE] ',
                           'validate': '[' + self.account + '][VALIDATE] ',
                           'create_cal': '[' + self.account + '][CREATE_CALENDAR] ',
                           'rem_cal': '[' + self.account + '][REMOVE_CALENDAR] ',
                           'list_cals': '[' + self.account + '][LIST_CALENDARS] ',
                           'create_evt': '[' + self.account + '][CREATE_EVENT] ',
                           'g_events': '[' + self.account + '][GET_EVENTS] ',
                           'g_cts': '[' + self.account + '][GET_CONTACTS] ',
                           'rem_ct': '[' + self.account + '][REMOVE_CONTACT] ',
                           'create_ct': '[' + self.account + '][CREATE_CONTACT] ',
                           'list_cts': '[' + self.account + '][LIST_CONTACTS] ',
                           'create_grp': '[' + self.account + '][CREATE_GROUP] ',
                           'list_grp': '[' + self.account + '][LIST_GROUPS] ',
                           'rem_grp': '[' + self.account + '][REMOVE_GROUP] ',
                           'format_ct': '[' + self.account + '][FORMAT_CONTACT] ',
                           'format_evt': '[' + self.account + '][FORMAT_EVENT] ',
                           'convert_dlist': '[' + self.account + '][CONVERT_DLIST] ',
                           'br_grps': '[' + self.account + '][BATCH_REMOVE_GROUPS] ',
                           'br_cts': '[' + self.account + '][BATCH_REMOVE_CONTACTS] ',
                           'br_cals': '[' + self.account + '][BATCH_REMOVE_CALENDARS] ',
                           'br_evts': '[' + self.account + '][BATCH_REMOVE_EVENTS] ',
                           'bi_grps': '[' + self.account + '][BATCH_INSERT_GROUPS] ',
                           'bi_cts': '[' + self.account + '][BATCH_INSERT_CONTACTS] ',
                           'bi_cals': '[' + self.account + '][BATCH_INSERT_CALENDARS] ',
                           'bi_evts': '[' + self.account + '][BATCH_INSERT_EVENTS] ',
                           'usr_update': '[' + self.account + '][USER_UPDATE] ',
                           'list_orgs': '[' + self.account + '][LIST_ORGANIZATIONS] ',
                           'user_info': '[' + self.account + '][USER_INFO] ',
                           'clean_prim': '[' + self.account + '][CLEAN_PRIMARY_CAL] ',
                           'ins_msg': '[' + self.account + '][INSERT_MESSAGE] ',
                           'gmail_info': '[' + self.account + '][GMAIL_INFO] ',
                           'gmail_conn': '[' + self.account + '][GMAIL_CONN] ',
                           'gmail_senders': '[' + self.account + '][GMAIL_SENDERS] ',
                           }

        self.is_ok = True

        # If has access to Google API
        if validate():
            # To access this account we need to delegate access using Google API
            if delegate():
                try:
                    # Calendar Token
                    self._service_calendar = build('calendar', 'v3', http=self.dc.authorize(Http()), cache_discovery=False)

                    # Contacts Token
                    self.service_contacs = ContactsClient(source='contacts_handler')
                    self.auth_token = gauth.OAuth2TokenFromCredentials(self.dc)
                    self.auth_token.authorize(self.service_contacs)
                    self.feed = ''
                except:
                    logger.exception('Problem when trying to access ' + self.account + ' account, check it in the logs later.')

            else:
                print ('Problem when trying to access ' + self.account + ' account, check it in the logs later.')
                self.is_ok = False
        else:
            print ('Problem when trying to validate your credentials, check it in the logs later.')
            self.is_ok = False