gpt4 book ai didi

javascript - 在 React Native 中从应用程序本身的 web View 重定向到移动应用程序

转载 作者:IT老高 更新时间:2023-10-28 23:34:55 26 4
gpt4 key购买 nike

我正在构建一个需要来自外部身份验证提供程序的身份验证的 Android 应用程序。所以我正在使用 react-native-oauth包来处理这个。

定义的redirect_uri 是一个深层链接,理想情况下,它应该在成功验证后自行打开我的应用程序。但WebView 似乎无法处理此重定向,我收到的响应是404-page not found。

这是我编写的用于处理身份验证的服务:

    const manager = new OAuthManager('<app_name>')

manager.addProvider({
'provider': {
auth_version: '2.0',
authorize_url:'<auth-url>',
access_token_url: '<auth-url>/token',
callback_url: 'http://localhost/provider',
}
});

manager.configure({
provider: {
client_id: '<id>',
client_secret: '<secret>',
redirect_uri: '<redirect-uri>' //DEEP LINK HERE
}
});
module.exports = {
authManager: () => {
manager.authorize('<provider>')
.then(resp => console.log(resp))
.catch(err => console.log(err));
}
}

我还定义了我的 Intent 过滤器,如 how to declare the deep links for your apps 上的 Android 文档中指定的那样。 . 使用应用组件中的 Linking.openURL() 打开深层链接时效果很好。

非常感谢任何帮助。

最佳答案

您不能直接将 redirect_uri 设置为您的移动应用程序(因为大多数身份验证提供程序不支持自定义 OAuth 方案)。

但您可以创建一些网页,接受来自 OAuth 提供商的重定向并打开您的应用(并发送所有重定向参数,例如 token)。

比如你创建页面https://example.com/oauth/,设置callback_urlhttps://example.com/oauth/XXXXX_provider,因此当用户被重定向到页面 https://example.com/oauth/XXXXX_provider&token=xxx 时,它将使用 appName://example/oauth 打开您的应用/google?token=xxx

您可以使用 Deeplink 处理 appName://example/oauth/google?token=xxx (当它安装在设备上时,它会打开您的移动应用程序)

处理重定向的页面示例:

<html><head></head><body>
<p>Please wait while we redirect you to Your APP NAME...</p>
<p><a href="javascript:redirectToApp()">Open appname</a></p>
<script>
var redirectToApp = function() {
var scheme = "appnameapp";
var openURL = "appname" + window.location.pathname + window.location.search + window.location.hash;
var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent);
var Android = /Android/.test(navigator.userAgent);
var newLocation;
if (iOS) {
newLocation = scheme + ":" + openURL;
} else if (Android) {
newLocation = "intent://" + openURL + "#Intent;scheme=" + scheme + ";package=com.appnameapp;end";
} else {
newLocation = scheme + "://" + openURL;
}
console.log(newLocation)
window.location.replace(newLocation);
}
window.onload = redirectToApp;
</script>


</body></html>

关于javascript - 在 React Native 中从应用程序本身的 web View 重定向到移动应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47017385/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com