Exemplo n.º 1
0
def index(request):
    storage = Storage(GmailCredentials, 'id', request.user, 'credential')
    credential = storage.get()
    if credential is None or credential.invalid == True:
        FLOW.params['state'] = xsrfutil.generate_token(settings.SECRET_KEY,
                                                   request.user)
        authorize_url = FLOW.step1_get_authorize_url()
        return HttpResponseRedirect(authorize_url)
    else:
        """
        http = httplib2.Http()
        http = credential.authorize(http)
        service = build("plus", "v1", http=http)
        people = service.people()
        peoplelist = people.list(collection='visible',
                                   userId='me').execute()
        logging.info(peoplelist)

        return render_to_response('gmail/welcome.html', {
                'peoplelist': peoplelist,
                })
        """
        imapobj = GMail_IMAP('imap.gmail.com')
        imapobj.login_oauth2(request.user.email, credential)
        messagelist = list(imapobj.gmsearch_messages('label:!projekt-vlab-test'))
        
        return render_to_response('gmail/welcome.html', {
                'messagelist': messagelist,
                })
Exemplo n.º 2
0
    def test_imap_freshtoken(self):
        storage = Storage("null.dat")
        credentials = run(self.flow, storage)

        os.unlink("null.dat")

        imap = GMail_IMAP()
        imap.login_oauth2(USERNAME, credentials=credentials)
        imap.select("INBOX")
Exemplo n.º 3
0
    def test_JWTServiceAccount(self):
        f = file(PRIVATE_KEY, "rb")
        key = f.read()
        f.close()

        credentials = SignedJwtAssertionCredentials(
            service_account_name=SERVICE_ACCOUNT_EMAIL, private_key=key, scope=" ".join(SCOPES), prn=USERNAME
        )

        imap = GMail_IMAP()
        imap.login_oauth2(USERNAME, credentials=credentials)

        imap.select("INBOX")
Exemplo n.º 4
0
    def test_imap_noaccesstoken(self):
        # the library should detect the lack of access token, and use the refresh token to go get a new one.
        self.credentials.access_token = None

        imap = GMail_IMAP()
        imap.debug = 4
        imap.login_oauth2(USERNAME, credentials=self.credentials)
        imap.select("INBOX")
Exemplo n.º 5
0
    def test_imap_noaccesstoken(self):
        # the library should detect the lack of access token, and use the refresh token to go get a new one.
        self.credentials.access_token = None

        imap = GMail_IMAP()
        imap.debug = 4
        imap.login_oauth2(USERNAME, credentials=self.credentials)
        imap.select("INBOX")
Exemplo n.º 6
0
    def test_imap_freshtoken(self):
        storage = Storage("null.dat")
        credentials = run(self.flow, storage)

        os.unlink("null.dat")

        imap = GMail_IMAP()
        imap.login_oauth2(USERNAME, credentials=credentials)
        imap.select("INBOX")
Exemplo n.º 7
0
    def test_imap_fromexpiredclientsecrets(self):

        # fake an expired token
        self.credentials.token_expiry = datetime.datetime(2012, 2, 10, 20, 59, 34)

        old_access_token = self.credentials.access_token

        imap = GMail_IMAP()
        imap.debug = 4
        imap.login_oauth2(USERNAME, credentials=self.credentials)
        imap.select("INBOX")

        self.assertNotEqual(old_access_token, imap._credentials.access_token, "We should have a new AccessToken")
Exemplo n.º 8
0
    def setUp(self):
        self.flow = flow_from_clientsecrets(filename=BASEDIR + "/client_secrets.json",
                                            scope=" ".join(SCOPES))

        storage = Storage("auth_secrets.dat")
        credentials = storage.get()
        if credentials is None or credentials.invalid:
            credentials = run(self.flow, storage)
        self.credentials = credentials

        imap = GMail_IMAP()
        imap.debug = 4
        imap.login_oauth2(USERNAME, credentials=self.credentials)
        imap.select("INBOX")
        self.imap = imap
Exemplo n.º 9
0
    def test_imap_fromexpiredclientsecrets(self):

        # fake an expired token
        self.credentials.token_expiry = datetime.datetime(2012, 2, 10, 20, 59, 34)

        old_access_token = self.credentials.access_token

        imap = GMail_IMAP()
        imap.debug = 4
        imap.login_oauth2(USERNAME, credentials=self.credentials)
        imap.select("INBOX")

        self.assertNotEqual(old_access_token, imap._credentials.access_token, "We should have a new AccessToken")
Exemplo n.º 10
0
    def setUp(self):
        self.flow = flow_from_clientsecrets(filename=BASEDIR + "/client_secrets.json", scope=" ".join(SCOPES))

        storage = Storage("auth_secrets.dat")
        credentials = storage.get()
        if credentials is None or credentials.invalid:
            credentials = run(self.flow, storage)
        self.credentials = credentials

        imap = GMail_IMAP()
        imap.debug = 4
        imap.login_oauth2(USERNAME, credentials=self.credentials)
        imap.select("INBOX")
        self.imap = imap
Exemplo n.º 11
0
    def test_JWTServiceAccount(self):
        f = file(PRIVATE_KEY, 'rb')
        key = f.read()
        f.close()

        credentials = SignedJwtAssertionCredentials(service_account_name=SERVICE_ACCOUNT_EMAIL,
                                                    private_key=key,
                                                    scope=" ".join(SCOPES),
                                                    prn=USERNAME)

        imap = GMail_IMAP()
        imap.login_oauth2(USERNAME, credentials=credentials)

        imap.select("INBOX")
Exemplo n.º 12
0
 def test_oauth2_token(self):
     username = "******"
     access_token = "hahah"
     auth_string = GMail_IMAP.generate_xoauth2_string(username, access_token)
     self.assertEqual(auth_string, "user=%s\1auth=Bearer %s\1\1" % (username, access_token))
Exemplo n.º 13
0
 def test_oauth2_token(self):
     username = "******"
     access_token = "hahah"
     auth_string = GMail_IMAP.generate_xoauth2_string(username, access_token)
     self.assertEqual(auth_string, 'user=%s\1auth=Bearer %s\1\1' % (username, access_token))