在 Flutter 中 url 请求后从响应中获取数据
Get data from response after url request in Flutter
我正在尝试在 Flutter 中实现 OAuth 1.0。
OAuth 1.0 的工作原理如下:
我的应用程序请求请求令牌,该令牌由我要对其进行身份验证的服务提供。然后用户被重定向到属于该服务的页面,在该页面上它授予访问我的应用程序数据的权限。现在我可以请求访问令牌,它将用于签署每个未来的数据请求。
我正在使用 this OAuth plugin for the OAuth procedure and Url Launcher plugin 来执行请求,因为第一个插件等待 pin 数据,而我希望用户享受完全自动化的过程。
我可以通过 OAuth 库请求请求令牌,但以下代码仅启动 url,因此我无法从响应中检索数据。
if (await canLaunch(url)) {
await launch(url, forceWebView: true, enableJavaScript: true);
} else {
throw 'Could not launch $url';
}
用户交互后如何获取数据?
你可以看看flutter_web_auth。我将其用于 Unsplash 的 OAuth 登录。
final url = Uri.https('unsplash.com', 'oauth/authorize', {
'response_type': 'code',
'client_id': client_id,
'redirect_uri': redirect_uri,
'scope': scode,
});
final result = await FlutterWebAuth.authenticate(
url: url
.toString()
.replaceAll("%2B", "+"),
callbackUrlScheme: "foobar");
您需要将“%2B”替换为 +,因为 url 生成器将 + 符号替换为 %2B。
final code = Uri.parse(result).queryParameters['code'];
您将获得您需要将其传递给 post 请求以获取令牌的代码。
当然还有其他用于 flutter 的 OAuth 提供程序,但我已经在我的一个应用程序中使用过它并且也对其进行了测试。
我接受 pradyot1996 的回答,但我给出了一些解释。
上面给出的提示让我更深入地了解了 OAuth 1.0a 和 plugin。我缺少的步骤是我可以添加一个重定向 uri,然后调用它来代替显示身份验证代码的网页。这是获取服务器验证码的最佳方式returns.
此外,this plugin, possibly like the one suggested by pradyot1996, permits to open a webpage inside of the app, without the need to ask the system to open a browser and start a server to listen to the answer. Though this is a bad practice, as stated here,我在我的应用程序中提供此选项以通知用户。也许我会添加这两个选项,crystal 清楚。
我正在尝试在 Flutter 中实现 OAuth 1.0。
OAuth 1.0 的工作原理如下:
我正在使用 this OAuth plugin for the OAuth procedure and Url Launcher plugin 来执行请求,因为第一个插件等待 pin 数据,而我希望用户享受完全自动化的过程。 我可以通过 OAuth 库请求请求令牌,但以下代码仅启动 url,因此我无法从响应中检索数据。
if (await canLaunch(url)) {
await launch(url, forceWebView: true, enableJavaScript: true);
} else {
throw 'Could not launch $url';
}
用户交互后如何获取数据?
你可以看看flutter_web_auth。我将其用于 Unsplash 的 OAuth 登录。
final url = Uri.https('unsplash.com', 'oauth/authorize', {
'response_type': 'code',
'client_id': client_id,
'redirect_uri': redirect_uri,
'scope': scode,
});
final result = await FlutterWebAuth.authenticate(
url: url
.toString()
.replaceAll("%2B", "+"),
callbackUrlScheme: "foobar");
您需要将“%2B”替换为 +,因为 url 生成器将 + 符号替换为 %2B。
final code = Uri.parse(result).queryParameters['code'];
您将获得您需要将其传递给 post 请求以获取令牌的代码。
当然还有其他用于 flutter 的 OAuth 提供程序,但我已经在我的一个应用程序中使用过它并且也对其进行了测试。
我接受 pradyot1996 的回答,但我给出了一些解释。
上面给出的提示让我更深入地了解了 OAuth 1.0a 和 plugin。我缺少的步骤是我可以添加一个重定向 uri,然后调用它来代替显示身份验证代码的网页。这是获取服务器验证码的最佳方式returns.
此外,this plugin, possibly like the one suggested by pradyot1996, permits to open a webpage inside of the app, without the need to ask the system to open a browser and start a server to listen to the answer. Though this is a bad practice, as stated here,我在我的应用程序中提供此选项以通知用户。也许我会添加这两个选项,crystal 清楚。