Flutter Android端实现Notion OAuth2的更佳方案:无需Firebase动态链接
替代Firebase动态链接的Notion OAuth2实现方案(Flutter Android)
核心问题回顾
Notion Auth API仅支持https://协议的重定向URL,使用Firebase动态链接时会丢失回调参数(如code),因此需要绕过动态链接,直接在App内监听授权响应。
方案一:Android App Links(直接映射HTTPS域名到App)
这是最贴合Notion要求的原生方案,无需中间层:
- 在AndroidManifest.xml中添加Intent Filter,指定接收
https://your-domain.com(替换为你的自有HTTPS域名)的回调请求:<intent-filter android:autoVerify="true"> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="https" android:host="your-domain.com" android:pathPrefix="/notion-oauth-callback" /> </intent-filter> - 在你的域名服务器根目录放置
assetlinks.json文件,完成App与域名的关联验证(确保Android系统认可该域名属于你的App)。 - 将Notion OAuth的
redirect_url设置为https://your-domain.com/notion-oauth-callback,授权完成后系统会直接打开你的App。 - 在Flutter中通过
Uri.base或getInitialUri()(配合app_links包)获取完整的回调URL,从中提取code参数。
方案二:App内嵌WebView直接处理授权流程
无需额外服务器,全程在App内完成:
- 使用
webview_flutter包嵌入WebView,加载Notion的授权URL(包含client_id、redirect_uri等参数,redirect_uri可设为你的自有HTTPS域名下的空白页面)。 - 配置WebView的
NavigationDelegate,拦截Notion的回调跳转:当WebView加载到redirect_uri时,直接从当前URL中提取code参数,终止跳转并关闭WebView。 - 或者在回调页面中添加JS代码,将
code参数通过WebView的JS通道传递给Flutter端。
方案三:后端中转转发回调参数
适合没有自有域名或不想配置App Links的场景:
- 搭建一个简单的后端接口(如Node.js/Go),地址设为
https://your-backend.com/notion-callback。 - 将Notion OAuth的
redirect_url设为该后端地址,后端收到code参数后,生成一个自定义Scheme链接(如myapp://notion/auth?code=xxx),返回一个自动跳转的HTML页面。 - 在AndroidManifest.xml中添加Scheme的Intent Filter:
<intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="myapp" android:host="notion" android:path="/auth" /> </intent-filter> - Flutter端通过
app_links或getInitialUri()监听Scheme链接,获取code参数。
内容的提问来源于stack exchange,提问作者Source30
相关产品推荐
相关产品推荐

