Apache Superset集成OAuth服务器遇token解析错误求助
解决Apache Superset集成自定义OAuth时的expires_at日期解析错误
问题原因
错误提示invalid literal for int() with base 10: '2024-02-26 15:22:13'明确说明:Flask-AppBuilder(Superset的底层安全框架)默认要求OAuth服务器返回的expires_at是Unix时间戳(秒级整数),但你的服务器返回的是格式化日期字符串,导致类型转换失败。
解决方案
有两种可行的处理方式,按需选择:
方式一:调整OAuth服务器的/token响应
直接修改自定义OAuth服务器的/token接口,将expires_at字段的值改为Unix时间戳(整数),例如把2024-02-26 15:22:13转换为对应的秒级时间戳1708956133。
方式二:自定义SecurityManager处理日期解析
如果你无法修改OAuth服务器的返回格式,可以在已有的CustomSsoSecurityManager中扩展逻辑,手动将日期字符串转换为时间戳:
- 修改自定义SecurityManager的代码:
from flask_appbuilder.security.manager import AUTH_OAUTH from superset.security import SupersetSecurityManager from datetime import datetime class CustomSsoSecurityManager(SupersetSecurityManager): def oauth_user_info(self, provider, response=None): # 调用父类方法获取基础用户信息 user_info = super().oauth_user_info(provider, response) # 针对自定义OAuth提供者处理expires_at if provider == 'your_custom_provider': # 替换为你的OAUTH_PROVIDERS中配置的name值 expires_at_str = response.get('expires_at') if expires_at_str and isinstance(expires_at_str, str): # 匹配你的日期格式,示例为'%Y-%m-%d %H:%M:%S',按需调整 dt = datetime.strptime(expires_at_str, '%Y-%m-%d %H:%M:%S') user_info['expires_at'] = int(dt.timestamp()) return user_info
- 在Superset配置文件
superset_config.py中确保使用该自定义管理器:
# 导入自定义SecurityManager(替换为实际的模块路径) from your_custom_module import CustomSsoSecurityManager AUTH_TYPE = AUTH_OAUTH OAUTH_PROVIDERS = [ { 'name': 'your_custom_provider', # 其他OAuth配置项(client_id、client_secret等) } ] CUSTOM_SECURITY_MANAGER = CustomSsoSecurityManager
注意:如果你的日期字符串包含时区信息(如
2024-02-26 15:22:13+00:00),需要调整strptime的格式字符串,或者使用datetime.fromisoformat()进行解析。
内容的提问来源于stack exchange,提问作者AidarDzhumagulov
相关产品推荐
相关产品推荐

