跨平台开发 - 使用 Google OAuth2 的限制
自 2022 年 1 月 4 日起,如果请求是在嵌入式浏览器组件(即 WebView)内发起的。
Google 的 OAuth 2.0 授权端点 (
https://accounts.google.com/o/oauth2/v2/auth
) 将开始返回 HTTP 404 错误。
根据 RFC 8252 - OAuth 2.0 for Native Apps 提案要求,本机应用不应使用 WebView 来进行授权请求。
这点在 Google 官方的博客中也有提及: Upcoming security changes to Google's OAuth 2.0 authorization endpoint in embedded webviews - Google Developers Blog。
Google 的主要考虑点包括:
- 安全性:WebView 容器由应用控制,用户无法验证当前授权页面是否被篡改,容易导致钓鱼攻击。
- 一致性:用户更熟悉系统浏览器/授权组件的界面,能直观确认授权域名。
- 隐私性:避免应用直接窃取用户输入的密码或拦截授权票据。
因此,WebView 授权模式已经被认为是过时且不安全的做法。
开发者应该优先使用以下方式来使用 OAth:
- 系统浏览器授权
- 打开外部浏览器加载 Google 授权页面。
- 使用 重定向 URI 回调结果(如自定义 URL Scheme、App Links、Universal Links)。
- 系统级授权组件
- Android:使用 Chrome Custom Tabs。
- iOS/macOS:使用 ASWebAuthenticationSession。
- 优点:安全、用户体验接近“内嵌页面”。
- 跨平台库与 SDK
- React Native: react-native-app-auth
- Flutter: flutter_appauth
- Cordova/Ionic: cordova-plugin-inappbrowser + AppAuth
- 这些库已经内置了 PKCE 支持 和回调处理逻辑,能快速接入。
P.S. 当我反馈这个问题给运营时,给我的解决方案是移动端单独提供一个无 OAuth2
跨平台开发 - 使用 Google OAuth2 的限制
http://www.inksha.com/archives/kua-ping-tai-kai-fa---shi-yong-google-oauth2-de-xian-zhi