| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- # -*- coding: utf-8 -*-
- # Part of Odoo. See LICENSE file for full copyright and licensing details.
- import json
- from odoo import http
- from odoo.http import request
- class GoogleOAuthController(http.Controller):
-
- @http.route('/web/google_oauth_callback', type='http', auth='public', website=True)
- def google_oauth_callback(self, **kw):
- """Handle Google OAuth callback for Google Drive integration"""
-
- # Get the authorization code from the callback
- code = kw.get('code')
- state = kw.get('state')
- error = kw.get('error')
-
- if error:
- return f"""
- <html>
- <head><title>OAuth Error</title></head>
- <body>
- <h1>OAuth Error</h1>
- <p>Error: {error}</p>
- <p><a href="/web">Return to Odoo</a></p>
- </body>
- </html>
- """
-
- if not code:
- return """
- <html>
- <head><title>OAuth Error</title></head>
- <body>
- <h1>OAuth Error</h1>
- <p>No authorization code received</p>
- <p><a href="/web">Return to Odoo</a></p>
- </body>
- </html>
- """
-
- try:
- # Get user settings ID from state (we pass the user settings ID as state)
- user_settings_id = int(state) if state else None
-
- if not user_settings_id:
- raise ValueError("No user settings ID provided in state")
-
- # Get user settings record
- user_settings = request.env['res.users.settings'].sudo().browse(user_settings_id)
- if not user_settings.exists():
- raise ValueError(f"User settings with ID {user_settings_id} not found")
-
- # Use Odoo's standard Google service for token exchange
- base_url = request.httprequest.url_root.strip('/') or request.env.user.get_base_url()
- redirect_uri = f'{base_url}/web/google_oauth_callback'
-
- # Get Google API credentials from system settings
- config = request.env['ir.config_parameter'].sudo()
- client_id = config.get_param('google_api.client_id', '')
- client_secret = config.get_param('google_api.client_secret', '')
-
- # Exchange authorization code for tokens using our own method
- access_token, refresh_token, expires_at = user_settings._exchange_authorization_code_for_tokens(
- code,
- client_id,
- client_secret,
- redirect_uri
- )
-
- # Store tokens in user settings
- user_settings._set_google_auth_tokens(access_token, refresh_token, expires_at)
-
- return f"""
- <html>
- <head><title>OAuth Success</title></head>
- <body>
- <h1>✅ Google Connect Success!</h1>
- <p>Successfully connected to Google services!</p>
- <p><a href="/web">Return to Odoo</a></p>
- </body>
- </html>
- """
-
- except Exception as e:
- return f"""
- <html>
- <head><title>OAuth Error</title></head>
- <body>
- <h1>❌ OAuth Error</h1>
- <p>Error: {str(e)}</p>
- <p><a href="/web">Return to Odoo</a></p>
- </body>
- </html>
- """
|