Exemplo n.º 1
0
 def getDocsClient(self):
     access_token = self.getAccessToken()
     if access_token is None: return None
     assert isinstance(access_token, OAuthHmacToken)
     docs_client = DocsClient()
     docs_client.auth_token = access_token
     return docs_client
Exemplo n.º 2
0
 def getDocsClient(self):
     access_token = self.getAccessToken()
     if access_token is None: return None
     assert isinstance(access_token, OAuthHmacToken)
     docs_client = DocsClient()
     docs_client.auth_token = access_token
     return docs_client
Exemplo n.º 3
0
    def _fetch_blips(self):
        assert self.auth_token, "auth_token must be set before we can fetch %s blips.  " \
                                "Please set username and password via the admin" % self.__class__.__name__ \

        import getpass

        from gdata.client import Unauthorized
        from gdata.docs.client import DocsClient
        from gdata.gauth import ClientLoginToken

        blips = []

        while True:
            try:
                client = DocsClient(source=self.application_name,
                                    auth_token=ClientLoginToken(
                                        self.auth_token))
                resources = client.GetAllResources()
                break
            except Unauthorized as e:
                msg = None
                for m in ('Token expired', 'Token invalid'):
                    if m in e.message:
                        msg = m
                if not msg:
                    raise e
                print "%s.  Please re-enter password for %s" % (msg,
                                                                self.email)
                self.password = getpass.getpass("Password: "******"We assume there is only one entry for each document currently"
            resource_atom = resource_atom.entries[0]

            revision_feed = client.get_revisions(resource)
            revision_atom = feedparser.parse(revision_feed.ToString())
            for revision in revision_atom.entries:
                timestamp = datetime.datetime.fromtimestamp(
                    time.mktime(revision.updated_parsed)).replace(tzinfo=utc)
                if timestamp > self.last_update:
                    #                    import pdb; pdb.set_trace()
                    blip = Blip()
                    #  some revisions don't have an author, use the document owner in that case
                    blip.who = getattr(revision, 'author',
                                       resource_atom.author)
                    blip.title = resource_atom.title
                    blip.summary = "%(title)s edited" % resource_atom
                    blip.source_url = resource_atom.link
                    blip.timestamp = timestamp

                    blips.append(blip)

        return blips
Exemplo n.º 4
0
 def docs_client(self):
     """ A DocsClient singleton, used to look up spreadsheets
     by name. """
     if not hasattr(self, '_docs_client'):
         client = DocsClient()
         client.ClientLogin(self.google_user, self.google_password,
                            SOURCE_NAME)
         self._docs_client = client
     return self._docs_client
Exemplo n.º 5
0
    def __init__(self, ui, service_name, file_title, mime_type="text/plain", folder=None):
        from gdata.docs.client import DocsClient
        self.service_name = service_name
        client = DocsClient(source=service_name)
        client.ssl = True

        self.ui = ui
        self.__authenticate(client)
        GoogleDocsConfigC.__init__(self, client, file_title, mime_type=mime_type, folder=folder)
        self.read()
Exemplo n.º 6
0
def two_legged(request):
    requestor_id = request.GET.get("user")
    if not requestor_id:
        requestor_id = request.session.get("openid_data", {}).get("email")
    if not requestor_id:
        return render(request, "core/two_legged_error.html", {"error": "Requestor Id is not defined"})
    client = DocsClient(source=settings.USER_AGENT)
    client.auth_token = TwoLeggedOAuthHmacToken(settings.CONSUMER_KEY, settings.CONSUMER_SECRET, requestor_id)
    try:
        documents = client.get_all_resources()
    except Unauthorized, e:
        return render(request, "core/two_legged_error.html", {"error": e})
Exemplo n.º 7
0
    def _fetch_blips(self):
        assert self.auth_token, "auth_token must be set before we can fetch %s blips.  " \
                                "Please set username and password via the admin" % self.__class__.__name__ \

        import getpass

        from gdata.client import Unauthorized
        from gdata.docs.client import DocsClient
        from gdata.gauth import ClientLoginToken

        blips = []

        while True:
            try:
                client = DocsClient(source=self.application_name, auth_token=ClientLoginToken(self.auth_token))
                resources = client.GetAllResources()
                break
            except Unauthorized as e:
                msg = None
                for m in ('Token expired', 'Token invalid'):
                    if m in e.message:
                        msg = m
                if not msg:
                    raise e
                print "%s.  Please re-enter password for %s" % (msg, self.email)
                self.password = getpass.getpass("Password: "******"We assume there is only one entry for each document currently"
            resource_atom = resource_atom.entries[0]

            revision_feed = client.get_revisions(resource)
            revision_atom = feedparser.parse(revision_feed.ToString())
            for revision in revision_atom.entries:
                timestamp = datetime.datetime.fromtimestamp(time.mktime(revision.updated_parsed)).replace(tzinfo=utc)
                if timestamp > self.last_update:
#                    import pdb; pdb.set_trace()
                    blip = Blip()
                    #  some revisions don't have an author, use the document owner in that case
                    blip.who = getattr(revision, 'author', resource_atom.author)
                    blip.title = resource_atom.title
                    blip.summary="%(title)s edited" % resource_atom
                    blip.source_url = resource_atom.link
                    blip.timestamp = timestamp

                    blips.append(blip)

        return blips
Exemplo n.º 8
0
def getDocsClient():
    client = DocsClient(source='odenkiapi')
    access_token = loadAccessToken()
    if access_token is not None:
        client.auth_token = access_token
    return client
Exemplo n.º 9
0
def oauth_gdocs(request, access_token):
    client = DocsClient(source=settings.USER_AGENT)
    client.auth_token = access_token
    documents = client.get_all_resources()
    return render(request, "core/oauth_gdocs.html", {"documents": documents})
Exemplo n.º 10
0
    def GET(self, jrequest, jresponse):
        assert isinstance(jrequest, JsonRpcRequest)
        assert isinstance(jresponse, JsonRpcResponse)
        jresponse.setId()
        assert jresponse.getId()
        debug("GET called with id=%s" % jresponse.getId())
        jresponse.setResultValue("gaesession", get_current_session())
        #odenkiSession = OdenkiSession()
        #jresponse.setResultValue("odenkiSession", odenkiSession)
        googleAuthSession = GoogleAuthSession()
        debug("googleAuthSession has keys %s" % googleAuthSession.keys())
        jresponse.setResultValue("googleAuthSession", googleAuthSession)

        if googleAuthSession.getAccessToken():
            debug("access token %s already exists" %
                  googleAuthSession.getAccessToken())
            debug("googleAuthSesson has keys %s" % googleAuthSession.keys())
            return

        if googleAuthSession.getNonAuthorizedRequestToken():
            assert googleAuthSession.getNonAuthorizedRequestToken(
            ).auth_state == REQUEST_TOKEN
            assert isinstance(googleAuthSession.getNonAuthorizedRequestToken(),
                              OAuthHmacToken)
            debug("Extracting authorized request token from callback URL = " +
                  self.request.url)
            authorized_request_token = AuthorizeRequestToken(
                googleAuthSession.getNonAuthorizedRequestToken(),
                self.request.url)
            if authorized_request_token.token is None:
                googleAuthSession.revoke()
                assert not googleAuthSession._getToken()
                error_message = "Can't extract authorized request token from the URL %s" % self.request.url
                debug(error_message)
                assert jresponse.getId()
                jresponse.setError(JsonRpcError.SERVER_ERROR_RESERVED_MIN,
                                   error_message)
                return
            assert authorized_request_token.token
            assert authorized_request_token.auth_state == AUTHORIZED_REQUEST_TOKEN
            googleAuthSession.setAuthorizedRequestToken(
                authorized_request_token)
            debug("%s, %s" %
                  (googleAuthSession.getAuthorizedRequestToken().token,
                   authorized_request_token.token))
            assert googleAuthSession.getAuthorizedRequestToken(
            ).token == authorized_request_token.token

        if googleAuthSession.getAuthorizedRequestToken():
            assert isinstance(googleAuthSession.getAuthorizedRequestToken(),
                              OAuthHmacToken)
            assert googleAuthSession.getAuthorizedRequestToken(
            ).auth_state == AUTHORIZED_REQUEST_TOKEN
            docs_client = DocsClient()
            try:
                access_token = docs_client.GetAccessToken(
                    googleAuthSession.getAuthorizedRequestToken())
            except Exception, e:
                error_message = "failed to exchange authorized request token to access token, %s" % e
                debug(error_message)
                googleAuthSession.revoke()
                jresponse.setError(JsonRpcError.SERVER_ERROR_RESERVED_MIN,
                                   error_message)
                return
            assert isinstance(access_token, OAuthHmacToken)
            assert access_token.auth_state == ACCESS_TOKEN
            googleAuthSession.setAccessToken(access_token)
            assert googleAuthSession.getAccessToken(
            ).token is access_token.token
            debug("access token = %s" % googleAuthSession.getAccessToken)
            debug(
                "after exchanging authorized request token with access token, googleAuthSession has keys %s"
                % googleAuthSession.keys())
            return
Exemplo n.º 11
0
class _GoogleRequestHandler(JsonRpcDispatcher):
    def revoke(self, jrequest, jresponse):
        assert isinstance(jrequest, JsonRpcRequest)
        assert isinstance(jresponse, JsonRpcResponse)
        jresponse.setResultValue("gaesession", get_current_session())
        googleAuthSession = GoogleAuthSession()
        googleAuthSession.revoke()
        jresponse.setResultValue("googleAuthSession", googleAuthSession)

    def GET(self, jrequest, jresponse):
        assert isinstance(jrequest, JsonRpcRequest)
        assert isinstance(jresponse, JsonRpcResponse)
        jresponse.setId()
        assert jresponse.getId()
        debug("GET called with id=%s" % jresponse.getId())
        jresponse.setResultValue("gaesession", get_current_session())
        #odenkiSession = OdenkiSession()
        #jresponse.setResultValue("odenkiSession", odenkiSession)
        googleAuthSession = GoogleAuthSession()
        debug("googleAuthSession has keys %s" % googleAuthSession.keys())
        jresponse.setResultValue("googleAuthSession", googleAuthSession)

        if googleAuthSession.getAccessToken():
            debug("access token %s already exists" %
                  googleAuthSession.getAccessToken())
            debug("googleAuthSesson has keys %s" % googleAuthSession.keys())
            return

        if googleAuthSession.getNonAuthorizedRequestToken():
            assert googleAuthSession.getNonAuthorizedRequestToken(
            ).auth_state == REQUEST_TOKEN
            assert isinstance(googleAuthSession.getNonAuthorizedRequestToken(),
                              OAuthHmacToken)
            debug("Extracting authorized request token from callback URL = " +
                  self.request.url)
            authorized_request_token = AuthorizeRequestToken(
                googleAuthSession.getNonAuthorizedRequestToken(),
                self.request.url)
            if authorized_request_token.token is None:
                googleAuthSession.revoke()
                assert not googleAuthSession._getToken()
                error_message = "Can't extract authorized request token from the URL %s" % self.request.url
                debug(error_message)
                assert jresponse.getId()
                jresponse.setError(JsonRpcError.SERVER_ERROR_RESERVED_MIN,
                                   error_message)
                return
            assert authorized_request_token.token
            assert authorized_request_token.auth_state == AUTHORIZED_REQUEST_TOKEN
            googleAuthSession.setAuthorizedRequestToken(
                authorized_request_token)
            debug("%s, %s" %
                  (googleAuthSession.getAuthorizedRequestToken().token,
                   authorized_request_token.token))
            assert googleAuthSession.getAuthorizedRequestToken(
            ).token == authorized_request_token.token

        if googleAuthSession.getAuthorizedRequestToken():
            assert isinstance(googleAuthSession.getAuthorizedRequestToken(),
                              OAuthHmacToken)
            assert googleAuthSession.getAuthorizedRequestToken(
            ).auth_state == AUTHORIZED_REQUEST_TOKEN
            docs_client = DocsClient()
            try:
                access_token = docs_client.GetAccessToken(
                    googleAuthSession.getAuthorizedRequestToken())
            except Exception, e:
                error_message = "failed to exchange authorized request token to access token, %s" % e
                debug(error_message)
                googleAuthSession.revoke()
                jresponse.setError(JsonRpcError.SERVER_ERROR_RESERVED_MIN,
                                   error_message)
                return
            assert isinstance(access_token, OAuthHmacToken)
            assert access_token.auth_state == ACCESS_TOKEN
            googleAuthSession.setAccessToken(access_token)
            assert googleAuthSession.getAccessToken(
            ).token is access_token.token
            debug("access token = %s" % googleAuthSession.getAccessToken)
            debug(
                "after exchanging authorized request token with access token, googleAuthSession has keys %s"
                % googleAuthSession.keys())
            return


#        request_token = AeLoad(REQUEST_TOKEN_KEY)
#        if request_token is not None:
#            rpc.addLog("Request token was loaded by AeLoad.")
#            rpc.setResultValule("requestToken", request_token.token)
#            rpc.setResultValule("requestTokenSecret", request_token.token_secret)
#            rpc.setResultValule("requestTokenStatus", request_token.auth_state)
#            rpc.setResultValule("sid", odenki_session.getSid())
#            assert request_token.auth_state == REQUEST_TOKEN
#            assert isinstance(request_token, OAuthHmacToken)
#            authorized_request_token = AuthorizeRequestToken(request_token, self.request.url)
#            assert isinstance(authorized_request_token, OAuthHmacToken)
#            assert authorized_request_token.auth_state == AUTHORIZED_REQUEST_TOKEN
#            AeSave(authorized_request_token, REQUEST_TOKEN_KEY)
#            debug("Authorized request token was saved.")
#            docs_client = DocsClient()
#            try:
#                access_token = docs_client.GetAccessToken(authorized_request_token)
#                assert isinstance(access_token, OAuthHmacToken)
#                assert access_token.auth_state == ACCESS_TOKEN
#                debug("access token = " + access_token.token + ", secret = " + access_token.token_secret)
#                self.googleUser.setAccessToken(access_token)
#                assert isinstance(self.googleUser.getAccessToken(), OAuthHmacToken)
#                assert self.googleUser.getAccessToken().auth_state == ACCESS_TOKEN
#                #self.response.set_status(200)
#                #self.response.headers["Content-Type"] = "text/plain; charset=ascii"
#                rpc.addLog("Access token was saved..")
#                return
#            except Exception, e:
#                #self.response.set_status(500)
#                #self.response.headers["Content-Type"] = "text/plain; charset=ascii"
#                self.googleUser.deleteAccessToken()
#                AeDelete(REQUEST_TOKEN_KEY)
#                rpc.addLog("Couldn't exchange request token to access token. Request token was deleted.")
#                #rpc.setResultValule("message", "Request token was deleted.")
#                return

        debug("obtaining non-authorized request token")
        assert not googleAuthSession.getAuthorizedRequestToken()
        assert not googleAuthSession.getNonAuthorizedRequestToken()
        docs_client = DocsClient()
        non_authorized_request_token = docs_client.GetOAuthToken(
            GOOGLE_OAUTH_SCOPES,
            'http://%s/api/google' % self.request.host,
            GOOGLE_OAUTH_CONSUMER_KEY,
            consumer_secret=GOOGLE_OAUTH_CONSUMER_SECRET)
        assert isinstance(non_authorized_request_token, OAuthHmacToken)
        googleAuthSession.setNonAuthorizedRequestToken(
            non_authorized_request_token)
        assert googleAuthSession.getNonAuthorizedRequestToken(
        ).token == non_authorized_request_token.token
        assert isinstance(non_authorized_request_token, OAuthHmacToken)
        authorization_url = non_authorized_request_token.generate_authorization_url(
        )  #google_apps_domain=None
        assert isinstance(unicode(authorization_url), unicode)
        googleAuthSession.setAuthorizationUrl(unicode(authorization_url))
Exemplo n.º 12
0
def getDocsClient():
    client = DocsClient(source='odenkiapi')
    access_token = loadAccessToken()
    if access_token is not None: 
        client.auth_token = access_token
    return client