2011-02-11 12:37:53 +00:00
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
2013-06-25 16:19:22 +00:00
from openerp . osv import osv
2013-06-25 10:58:19 +00:00
from openerp import SUPERUSER_ID
2013-06-27 14:37:13 +00:00
from openerp . tools . translate import _
2013-12-02 14:31:33 +00:00
from openerp . addons . web . http import request
2013-06-25 10:58:19 +00:00
import urllib
2013-06-27 14:37:13 +00:00
import urllib2
2013-06-25 10:58:19 +00:00
import simplejson
2013-07-22 14:30:34 +00:00
class google_service ( osv . osv_memory ) :
2013-06-27 14:37:13 +00:00
_name = ' google.service '
2013-06-25 10:58:19 +00:00
2013-06-27 14:37:13 +00:00
def generate_refresh_token ( self , cr , uid , service , authorization_code , context = None ) :
2013-10-16 16:39:08 +00:00
ir_config = self . pool [ ' ir.config_parameter ' ]
client_id = ir_config . get_param ( cr , SUPERUSER_ID , ' google_ %s _client_id ' % service )
client_secret = ir_config . get_param ( cr , SUPERUSER_ID , ' google_ %s _client_secret ' % service )
redirect_uri = ir_config . get_param ( cr , SUPERUSER_ID , ' google_redirect_uri ' )
2013-06-25 10:58:19 +00:00
2013-10-16 16:39:08 +00:00
#Get the Refresh Token From Google And store it in ir.config_parameter
headers = { " Content-type " : " application/x-www-form-urlencoded " }
data = dict ( code = authorization_code , client_id = client_id , client_secret = client_secret , redirect_uri = redirect_uri , grant_type = " authorization_code " )
data = urllib . urlencode ( data )
try :
req = urllib2 . Request ( " https://accounts.google.com/o/oauth2/token " , data , headers )
content = urllib2 . urlopen ( req ) . read ( )
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 )
2013-06-27 14:37:13 +00:00
2013-10-16 16:39:08 +00:00
content = simplejson . loads ( content )
2013-06-27 14:37:13 +00:00
return content . get ( ' refresh_token ' )
2013-06-25 10:58:19 +00:00
2013-07-22 14:30:34 +00:00
def _get_google_token_uri ( self , cr , uid , service , scope , context = None ) :
2013-06-25 16:19:22 +00:00
ir_config = self . pool [ ' ir.config_parameter ' ]
params = {
2013-07-22 14:30:34 +00:00
' scope ' : scope ,
2013-06-25 16:19:22 +00:00
' 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 ' ,
2013-12-02 14:31:33 +00:00
' client_id ' : ir_config . get_param ( cr , SUPERUSER_ID , ' google_ %s _client_id ' % service ) ,
2013-06-25 16:19:22 +00:00
}
uri = ' https://accounts.google.com/o/oauth2/auth? %s ' % urllib . urlencode ( params )
return uri
2013-12-02 14:31:33 +00:00
#If no scope is passed, we use service by default to get a default scope
2013-12-11 13:06:35 +00:00
2013-12-09 14:08:19 +00:00
def _get_authorize_uri ( self , cr , uid , from_url , service , scope = False , context = None ) :
2013-12-11 13:06:35 +00:00
""" This method return the url needed to allow this instance of OpenErp to access to the scope of gmail specified as parameters """
2013-12-02 14:31:33 +00:00
state_obj = { }
state_obj [ ' d ' ] = cr . dbname
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 , ) ,
2013-12-19 14:25:14 +00:00
' redirect_uri ' : base_url + ' /google_account/authentication ' ,
#'approval_prompt':'force',
2013-12-02 14:31:33 +00:00
' access_type ' : ' offline '
}
uri = self . get_uri_oauth ( a = ' auth ' ) + " ? %s " % urllib . urlencode ( params )
return uri
2013-12-11 13:06:35 +00:00
def _get_google_token_json ( self , cr , uid , authorize_code , service , context = None ) :
2013-12-02 14:31:33 +00:00
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 ' ,
2013-12-19 14:25:14 +00:00
#'redirect_uri': base_url + '/google_account/authentication'
2013-12-02 14:31:33 +00:00
}
headers = { " content-type " : " application/x-www-form-urlencoded " }
try :
data = urllib . urlencode ( params )
req = urllib2 . Request ( self . get_uri_oauth ( a = ' token ' ) , data , headers )
2013-12-11 13:06:35 +00:00
2013-12-02 14:31:33 +00:00
content = urllib2 . urlopen ( req ) . read ( )
res = simplejson . loads ( content )
except urllib2 . HTTPError , e :
print e
2013-12-11 13:06:35 +00:00
raise self . pool . get ( ' res.config.settings ' ) . get_config_warning ( cr , _ ( " Something went wrong during your token generation. Maybe your Authorization Code is invalid " ) , context = context )
2013-12-02 14:31:33 +00:00
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
2013-12-13 16:27:52 +00:00
# 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 "#########################################"
2013-12-02 14:31:33 +00:00
try :
2013-12-09 14:08:19 +00:00
if type . upper ( ) == ' GET ' or type . upper ( ) == ' DELETE ' :
2013-12-02 14:31:33 +00:00
data = urllib . urlencode ( params )
2013-12-09 14:08:19 +00:00
req = urllib2 . Request ( self . get_uri_api ( ) + uri + " ? " + data )
elif type . upper ( ) == ' POST ' or type . upper ( ) == ' PATCH ' or type . upper ( ) == ' PUT ' :
req = urllib2 . Request ( self . get_uri_api ( ) + uri , params , headers )
2013-12-02 14:31:33 +00:00
else :
2013-12-09 14:08:19 +00:00
raise ( ' Method not supported [ %s ] not in [GET, POST, PUT or PATCH]! ' % ( type ) )
req . get_method = lambda : type . upper ( )
2013-12-11 13:06:35 +00:00
2013-12-09 14:08:19 +00:00
request = urllib2 . urlopen ( req )
2013-12-11 13:06:35 +00:00
if request . getcode ( ) == 204 : #No content returned, (ex: POST calendar/event/clear)
2013-12-09 14:08:19 +00:00
res = True
2013-12-11 13:06:35 +00:00
elif request . getcode ( ) == 404 : #Page not found
res = False
2013-12-09 14:08:19 +00:00
else :
content = request . read ( )
res = simplejson . loads ( content )
2013-12-13 16:27:52 +00:00
print " = "
2013-12-02 14:31:33 +00:00
except urllib2 . HTTPError , e :
2013-12-09 14:08:19 +00:00
print " ERROR CATCHED : " , e . read ( )
2013-12-11 13:06:35 +00:00
raise self . pool . get ( ' res.config.settings ' ) . get_config_warning ( cr , _ ( " Something went wrong with your request to google " ) , context = context )
2013-12-02 14:31:33 +00:00
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 ) :
2013-12-17 12:40:28 +00:00
return self . pool . get ( ' ir.config_parameter ' ) . get_param ( cr , uid , ' google_ %s _client_id ' % ( service , ) , default = False , context = context )
2013-12-02 14:31:33 +00:00
def get_client_secret ( self , cr , uid , service , context = None ) :
2013-12-17 12:40:28 +00:00
return self . pool . get ( ' ir.config_parameter ' ) . get_param ( cr , uid , ' google_ %s _client_secret ' % ( service , ) , default = False , context = context )
2013-12-02 14:31:33 +00:00
2013-12-11 13:06:35 +00:00
def get_uri_oauth ( self , a = ' ' ) : #a = optional action
2013-12-02 14:31:33 +00:00
return " https://accounts.google.com/o/oauth2/ %s " % ( a , )
def get_uri_api ( self ) :
return ' https://www.googleapis.com '