main.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. import json
  4. from odoo import http
  5. from odoo.http import request
  6. class GoogleOAuthController(http.Controller):
  7. @http.route('/web/google_oauth_callback', type='http', auth='public', website=True)
  8. def google_oauth_callback(self, **kw):
  9. """Handle Google OAuth callback for Google Drive integration"""
  10. # Get the authorization code from the callback
  11. code = kw.get('code')
  12. state = kw.get('state')
  13. error = kw.get('error')
  14. if error:
  15. return f"""
  16. <html>
  17. <head><title>OAuth Error</title></head>
  18. <body>
  19. <h1>OAuth Error</h1>
  20. <p>Error: {error}</p>
  21. <p><a href="/web">Return to Odoo</a></p>
  22. </body>
  23. </html>
  24. """
  25. if not code:
  26. return """
  27. <html>
  28. <head><title>OAuth Error</title></head>
  29. <body>
  30. <h1>OAuth Error</h1>
  31. <p>No authorization code received</p>
  32. <p><a href="/web">Return to Odoo</a></p>
  33. </body>
  34. </html>
  35. """
  36. try:
  37. # Get user settings ID from state (we pass the user settings ID as state)
  38. user_settings_id = int(state) if state else None
  39. if not user_settings_id:
  40. raise ValueError("No user settings ID provided in state")
  41. # Get user settings record
  42. user_settings = request.env['res.users.settings'].sudo().browse(user_settings_id)
  43. if not user_settings.exists():
  44. raise ValueError(f"User settings with ID {user_settings_id} not found")
  45. # Use Odoo's standard Google service for token exchange
  46. base_url = request.httprequest.url_root.strip('/') or request.env.user.get_base_url()
  47. redirect_uri = f'{base_url}/web/google_oauth_callback'
  48. # Get Google API credentials from system settings
  49. config = request.env['ir.config_parameter'].sudo()
  50. client_id = config.get_param('google_api.client_id', '')
  51. client_secret = config.get_param('google_api.client_secret', '')
  52. # Exchange authorization code for tokens using our own method
  53. access_token, refresh_token, expires_at = user_settings._exchange_authorization_code_for_tokens(
  54. code,
  55. client_id,
  56. client_secret,
  57. redirect_uri
  58. )
  59. # Store tokens in user settings
  60. user_settings._set_google_auth_tokens(access_token, refresh_token, expires_at)
  61. return f"""
  62. <html>
  63. <head><title>OAuth Success</title></head>
  64. <body>
  65. <h1>✅ Google Connect Success!</h1>
  66. <p>Successfully connected to Google services!</p>
  67. <p><a href="/web">Return to Odoo</a></p>
  68. </body>
  69. </html>
  70. """
  71. except Exception as e:
  72. return f"""
  73. <html>
  74. <head><title>OAuth Error</title></head>
  75. <body>
  76. <h1>❌ OAuth Error</h1>
  77. <p>Error: {str(e)}</p>
  78. <p><a href="/web">Return to Odoo</a></p>
  79. </body>
  80. </html>
  81. """