diff options
| author | jke-openerp <jke@openerp.com> | 2013-12-02 15:31:33 +0100 |
|---|---|---|
| committer | jke-openerp <jke@openerp.com> | 2013-12-02 15:31:33 +0100 |
| commit | 27a42cc6eb606cb353dba71a2c7be0e83eda89cf (patch) | |
| tree | c11718b33230c6ba810e3929dd614179710fdd2c /addons/google_base_account | |
| parent | 74d698f42f7c377d0f01595904c0c68851c030d1 (diff) | |
[IMP] google oAuth2 + start synchro calendar (insert ok, bug with recurrent)
bzr revid: jke@openerp.com-20131202143133-u6vi1xw1oa9w8qpf
Diffstat (limited to 'addons/google_base_account')
| -rw-r--r-- | addons/google_base_account/__init__.py | 2 | ||||
| -rw-r--r-- | addons/google_base_account/controllers/__init__.py | 1 | ||||
| -rw-r--r-- | addons/google_base_account/controllers/main.py | 119 | ||||
| -rw-r--r-- | addons/google_base_account/google_base_account.py | 133 |
4 files changed, 251 insertions, 4 deletions
diff --git a/addons/google_base_account/__init__.py b/addons/google_base_account/__init__.py index 44a5aabe277..ee900c8febc 100644 --- a/addons/google_base_account/__init__.py +++ b/addons/google_base_account/__init__.py @@ -18,8 +18,8 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. # ############################################################################## - import google_base_account +import controllers # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/google_base_account/controllers/__init__.py b/addons/google_base_account/controllers/__init__.py new file mode 100644 index 00000000000..8ee9bae18d9 --- /dev/null +++ b/addons/google_base_account/controllers/__init__.py @@ -0,0 +1 @@ +import main diff --git a/addons/google_base_account/controllers/main.py b/addons/google_base_account/controllers/main.py new file mode 100644 index 00000000000..ce7909da6cf --- /dev/null +++ b/addons/google_base_account/controllers/main.py @@ -0,0 +1,119 @@ +import simplejson +import urllib +import openerp +import openerp.addons.web.http as http +from openerp.addons.web.http import request +import openerp.addons.web.controllers.main as webmain +import json +from openerp.addons.web.http import SessionExpiredException +from werkzeug.exceptions import BadRequest +import werkzeug.utils + +class google_auth(http.Controller): + + @http.route('/googleauth/oauth2callback', type='http', auth="none") + def oauth2callback(self, **kw): + + state = simplejson.loads(kw['state']) + + #action = state.get('a') + #menu = state.get('m') + dbname = state.get('d') + #service = state.get('s') + url_return = state.get('from') + + registry = openerp.modules.registry.RegistryManager.get(dbname) + with registry.cursor() as cr: + #TODO CHECK IF REQUEST OK + registry.get('google.calendar').set_all_tokens(cr,request.session.uid,kw['code']) + registry.get('google.calendar').set_primary_id(cr,request.session.uid) + + return werkzeug.utils.redirect(url_return) + + #@openerp.addons.web.http.route('/web_calendar_sync/sync_calendar/sync_data', type='json', auth='user') + @http.route('/web_calendar_sync/sync_calendar/sync_data', type='json', auth='user') + def sync_data(self, arch, fields, model,**kw): + calendar_info = { + 'field_data':{}, + 'date_start':arch['attrs'].get('date_start'), + 'date_stop':arch['attrs'].get('date_stop'), + 'calendar_string':arch['attrs'].get('string'), + 'model':model + } + for field, data in fields.items(): + calendar_info['field_data'][field] = { + 'type': data.get('type'), + 'string': data.get('string') + } + + if model == 'crm.meeting': + model_obj = request.registry.get('crm.meeting.synchronize') + gc_obj = request.registry.get('google.calendar') + + #We check that user has already accepted openerp to acces his calendar ! + if not gc_obj.get_refresh_token(request.cr, request.uid,context=kw.get('LocalContext')): + url = gc_obj.authorize_google_uri(request.cr, request.uid, from_url=kw.get('fromurl'),context=kw.get('LocalContext')) + return { + "status" : "NeedAuth", + "url" : url + } + + #We lunch th synchronization + print "ORI COONTEXT = ",kw.get('LocalContext') + model_obj.synchronize_events(request.cr, request.uid, [], kw.get('LocalContext')) + else: + model_obj = request.registry.get('google.calendar') + model_obj.synchronize_calendar(request.cr, request.uid, calendar_info, kw.get('LocalContext')) + + + return { "status" : "SUCCESS" } + + + + + + + + + + + + + + + + + + + + + + + + + + + @http.route('/googleauth/AuthorizeMe', type='http', auth="none") + def authorize_app(self,**val): + if val.get('done'): + return; + registry = openerp.modules.registry.RegistryManager.get(request.session.get('db')) + gs_pool = registry.get('google.service') + with registry.cursor() as cr: + url = gs_pool._get_authorize_uri(cr,request.session.uid,service='calendar',from_url='') + return werkzeug.utils.redirect(url) ##REDIRECT WHERE THE USER WAS BEFORE (with state) + + + @http.route('/googleauth/GiveMeAToken', type='http', auth="none") + def authorize_me(self,**val): + registry = openerp.modules.registry.RegistryManager.get(request.session.get('db')) + gs_pool = registry.get('google.service') + with registry.cursor() as cr: + token = gs_pool._get_google_token_json(cr, request.session.uid, 'api_code') + + print '#####################################' + print '## YOUR TOKEN : ',token, " ##" + print '#####################################' + #return werkzeug.utils.redirect(url) + return + diff --git a/addons/google_base_account/google_base_account.py b/addons/google_base_account/google_base_account.py index 4f85db0582b..e45a599be51 100644 --- a/addons/google_base_account/google_base_account.py +++ b/addons/google_base_account/google_base_account.py @@ -22,6 +22,7 @@ from openerp.osv import osv from openerp import SUPERUSER_ID from openerp.tools.translate import _ +from openerp.addons.web.http import request import urllib import urllib2 @@ -57,9 +58,135 @@ class google_service(osv.osv_memory): 'redirect_uri': ir_config.get_param(cr, SUPERUSER_ID, 'google_redirect_uri'), 'client_id': ir_config.get_param(cr, SUPERUSER_ID, 'google_%s_client_id' % service), 'response_type': 'code', - 'client_id': ir_config.get_param(cr, SUPERUSER_ID, 'google_%s_client_id' % service), + 'client_id': ir_config.get_param(cr, SUPERUSER_ID, 'google_%s_client_id' % service), } uri = 'https://accounts.google.com/o/oauth2/auth?%s' % urllib.urlencode(params) return uri - -# vim:expandtab:smartindent:toabstop=4:softtabstop=4:shiftwidth=4: + + #If no scope is passed, we use service by default to get a default scope + + def _get_authorize_uri(self, cr, uid, from_url, service, scope = False, context=None): #authorize_API + if not service: + print 'please specify a service (Calendar for example)!' + + state_obj = {} + state_obj['d'] = cr.dbname + state_obj['a'] = request and request.params.get('action') or False + state_obj['m'] = request and request.params.get('menu_id') or False + state_obj['s'] = service + state_obj['from'] = from_url + + base_url = self.get_base_url(cr, uid, context) + client_id = self.get_client_id(cr, uid, service, context) + + params = { + 'response_type': 'code', + 'client_id': client_id, + 'state' : simplejson.dumps(state_obj), + 'scope': scope or 'https://www.googleapis.com/auth/%s' % (service,), + 'redirect_uri': base_url + '/googleauth/oauth2callback', + 'approval_prompt':'force', + 'access_type':'offline' + } + + uri = self.get_uri_oauth(a='auth') + "?%s" % urllib.urlencode(params) + return uri + + def _get_google_token_json(self, cr, uid, authorize_code, service, context=None): #exchange_AUTHORIZATION vs Token (service = calendar) + res = False + base_url = self.get_base_url(cr, uid, context) + client_id = self.get_client_id(cr, uid, service, context) + client_secret = self.get_client_secret(cr, uid, service, context) + + params = { + 'code': authorize_code, + 'client_id': client_id, + 'client_secret': client_secret, + 'grant_type' : 'authorization_code', + 'redirect_uri': base_url + '/googleauth/oauth2callback' + } + + headers = {"content-type": "application/x-www-form-urlencoded"} + + try: + data = urllib.urlencode(params) + req = urllib2.Request(self.get_uri_oauth(a='token'), data, headers) + content = urllib2.urlopen(req).read() + res = simplejson.loads(content) + + except urllib2.HTTPError,e: + print e + raise self.pool.get('res.config.settings').get_config_warning(cr, _("Something went wrong during your token generation. Maybe your Authorization Code is invalid or already expired"), context=context) + + return res + + def _refresh_google_token_json(self, cr, uid, refresh_token, service, context=None): #exchange_AUTHORIZATION vs Token (service = calendar) + res = False + base_url = self.get_base_url(cr, uid, context) + client_id = self.get_client_id(cr, uid, service, context) + client_secret = self.get_client_secret(cr, uid, service, context) + + params = { + 'refresh_token': refresh_token, + 'client_id': client_id, + 'client_secret': client_secret, + 'grant_type' : 'refresh_token' + } + + headers = {"content-type": "application/x-www-form-urlencoded"} + + try: + data = urllib.urlencode(params) + req = urllib2.Request(self.get_uri_oauth(a='token'), data, headers) + content = urllib2.urlopen(req).read() + res = simplejson.loads(content) + except urllib2.HTTPError: + raise self.pool.get('res.config.settings').get_config_warning(cr, _("Something went wrong during your token generation. Maybe your Authorization Code is invalid or already expired"), context=context) + + return res + + def _do_request(self,cr,uid,uri,params={},headers={},type='POST', context=None): + res = False + + print "#########################################" + print "### URI : %s ###" % (uri) + print "### HEADERS : %s ###" % (headers) + print "### METHOD : %s ###" % (type) + if type=='GET': + print "### PARAMS : %s ###" % urllib.urlencode(params) + else: + print "### PARAMS : %s ###" % (params) + + print "#########################################" + + try: + if type.upper() == 'GET': + data = urllib.urlencode(params) + req = urllib2.Request(self.get_uri_api() + uri + "?" + data)#,headers) + elif type.upper() == 'POST': + req = urllib2.Request(self.get_uri_api() + uri, params, headers) + else: + raise ('Method not supported [GET or POST] not in [%s]!' % (type)) + + content = urllib2.urlopen(req).read() + res = simplejson.loads(content) + except urllib2.HTTPError,e: + print e.read() + raise self.pool.get('res.config.settings').get_config_warning(cr, _("Something went wrong during your token generation. Maybe your Authorization Code is invalid or already expired"), context=context) + + return res + + def get_base_url(self, cr, uid, context=None): + return self.pool.get('ir.config_parameter').get_param(cr, uid, 'web.base.url',default='http://www.openerp.com?NoBaseUrl',context=context) + + def get_client_id(self, cr, uid, service, context=None): + return self.pool.get('ir.config_parameter').get_param(cr, uid, 'google_%s_client_id' % (service,),context=context) + + def get_client_secret(self, cr, uid, service, context=None): + return self.pool.get('ir.config_parameter').get_param(cr, uid, 'google_%s_client_secret' % (service,),context=context) + + def get_uri_oauth(self,a=''): #a = optionnal action + return "https://accounts.google.com/o/oauth2/%s" % (a,) + + def get_uri_api(self): + return 'https://www.googleapis.com' |
