mirror of
https://gitlab.com/flectra-hq/flectra.git
synced 2026-11-02 20:30:20 -06:00
[PATCH] Upstream patch - 28012022
This commit is contained in:
1 parent
f882f983ab
commit
abc96c91ca
44 files changed
+1170
-345
No files matched your search
@@ -9,6 +9,7 @@ import requests
|
||||
from werkzeug import urls
|
||||
|
||||
from flectra import api, fields, models, _
|
||||
from flectra.exceptions import UserError
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -119,6 +120,42 @@ class GoogleService(models.AbstractModel):
|
||||
error_msg = _("Something went wrong during your token generation. Maybe your Authorization Code is invalid")
|
||||
raise self.env['res.config.settings'].get_config_warning(error_msg)
|
||||
|
||||
@api.model
|
||||
def _get_access_token(self, refresh_token, service, scope):
|
||||
"""Fetch the access token thanks to the refresh token."""
|
||||
get_param = self.env['ir.config_parameter'].sudo().get_param
|
||||
client_id = get_param('google_%s_client_id' % service, default=False)
|
||||
client_secret = get_param('google_%s_client_secret' % service, default=False)
|
||||
|
||||
if not client_id or not client_secret:
|
||||
raise UserError(_('Google %s is not yet configured.', service.title()))
|
||||
|
||||
if not refresh_token:
|
||||
raise UserError(_('The refresh token for authentication is not set.'))
|
||||
|
||||
try:
|
||||
result = requests.post(
|
||||
GOOGLE_TOKEN_ENDPOINT,
|
||||
data={
|
||||
'client_id': client_id,
|
||||
'client_secret': client_secret,
|
||||
'refresh_token': refresh_token,
|
||||
'grant_type': 'refresh_token',
|
||||
'scope': scope,
|
||||
},
|
||||
headers={'Content-type': 'application/x-www-form-urlencoded'},
|
||||
timeout=TIMEOUT,
|
||||
)
|
||||
result.raise_for_status()
|
||||
except requests.HTTPError:
|
||||
raise UserError(
|
||||
_('Something went wrong during the token generation. Please request again an authorization code.')
|
||||
)
|
||||
|
||||
json_result = result.json()
|
||||
|
||||
return json_result.get('access_token'), json_result.get('expires_in')
|
||||
|
||||
@api.model
|
||||
def _do_request(self, uri, params=None, headers=None, method='POST', preuri="https://www.googleapis.com", timeout=TIMEOUT):
|
||||
""" Execute the request to Google API. Return a tuple ('HTTP_CODE', 'HTTP_RESPONSE')
|
||||
|
||||
Reference in new issue
Block a user