You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 15:05:31