是否可以使用 Android 应用程序链接,从https://
例如:https://my-app.com/callback
在 OAuth2 流程结束时从 Android WebView 重定向回我的应用程序?我知道正常的深度链接是如何工作的,例如com.my-app://
or my-app://
可用于重定向回我的应用程序。根据我的理解,WebView 不知道如何处理此类协议,将请求传递给操作系统,然后操作系统将请求传递给我的应用程序,如果在中提供了足够的 IntentFilter,则该应用程序处理此 urlAndroidManifest.xml
.
这可以通过https://
方案或重定向将始终被 WebView 捕获,并且无法重定向回我的应用程序?
指定我想要通过步骤实现的目标:
- 中提供了一个 IntentFilter
AndroidManifest.xml
处理应用程序链接,例如:
<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="http" android:host="my-app.com/callback" />
<data android:scheme="https" />
</intent-filter>
- A valid
assetlinks.json
提供于https://my-app.com/.well-known/assetlinks.json
(此时,IntentFilterIntent
日志显示 JSON 验证成功,我可以使用如下命令从终端打开应用程序npx uri-scheme open https://my-app.com/callback
)
- 我的应用程序通过启动来启动 OAuth2 流程
CustomTabsIntent.launchUrl
网址如下:
https://accounts.google.com/o/oauth2/v2/auth?
scope=email%20profile&
response_type=code&
state=state&
redirect_uri=https://my-app.com/callback&
client_id=client_id
完成这些步骤后,我希望我的应用程序在成功登录后打开,因为它是 url 的有效处理程序,并且不希望卡在浏览器中。这可能吗,或者请求永远不会从浏览器转发到操作系统,因为浏览器是有效的处理程序https://
scheme?
如果上述不可能,有没有办法从 WebView 导航回应用程序,提供auth_code
或者做到这一点的唯一方法是使用自定义方案?
专门针对 Android 12 及更高版本
- 在 Android 12 中,Google 改变了解决 Web Intent 的方式。如果您的目标是 Android 12 或更高版本,则需要实现特定功能。了解更多相关信息HERE https://developer.android.com/about/versions/12/behavior-changes-all#web-intent-resolution
OAuth 最佳实践
- 如中所述互联网工程任务组针对本机应用程序的 OAuth 2.0 https://www.rfc-editor.org/rfc/rfc8252:
For authorizing users in native apps, the best current practice is to perform the OAuth authorization request in an external user-agent(typically the browser) rather than an embedded user-agent (such as one implemented with web-views
- 您的申请应该
NOT
使用 Android WebView。相反,使用隐式意图将用户重定向到浏览器进行登录
重定向URI
- 在意图过滤器内定义重定向 URI 时,它们应该遵循这个图案 https://developer.android.com/guide/topics/manifest/data-element:
<scheme>://<host>:<port>[<path>|<pathPrefix>|<pathPattern>|<pathAdvancedPattern>|<pathSuffix>]
- 如果您的重定向 URI 为
https://my-app.com/callback
。意图过滤器将是:
<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:host="my-app.com"
android:scheme="https"
android:path="/callback"
/>
</intent-filter>
Example
- 我写了一篇博文,HERE https://dev.to/theplebdev/oauth20-and-android-login-with-github-and-get-the-authorization-code-40n3,关于在 Android 中使用 OAuth 与 GitHub API
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)