我正在尝试使用Flutter Dart通过基于Kevin Segaud的博客文章以及Trello授权文档的url_launcher软件包对Trello进行身份验证。
我的代码确实显示了Trello授权页面,并且它调用了回调URL,但是我无法获得令牌(作为URL片段传递的令牌)不可用于HTTP请求。但是,在屏幕上,它清晰地显示了带有令牌的URL作为URL中的片段。
class TrelloAuthenticator {
Future<String> authorize() async {
Stream<String> onCode = await _server();
var url = "https://trello.com/1/authorize?expiration=30days&name=TestApp&callback_method=fragment&scope=read,write&return_url=http://localhost:8080&key=ab1234ab1234ab1234ab1234";
print("modifiedUrl:$url");
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
final String token = await onCode.first;
return token;
}
Future<Stream<String>> _server() async {
final StreamController<String> onCode = new StreamController();
HttpServer server = await HttpServer.bind(InternetAddress.loopbackIPv4, 8080);
server.listen((HttpRequest request) async {
print("_server method:${request.method}");
print("_server contentType:${request.headers.contentType}");
print("_server requestedUri:${request.requestedUri}");
print("_server requestedUri hasFragment:${request.requestedUri.hasFragment}");
print("_server requestedUri fragment:${request.requestedUri.fragment}");
print("_server requestedUri data:${request.requestedUri.data}");
print("_server requestedUri origin:${request.requestedUri.origin}");
print("_server requestedUri query:${request.requestedUri.query}");
print("_server requestedUri queryParameters:${request.requestedUri.queryParameters}");
print("_server requestedUri normalizePath:${request.requestedUri.normalizePath()}");
print("_server requestedUri normalizePath queryParametersAll:${request.requestedUri.normalizePath().queryParametersAll}");
print("_server requestedUri normalizePath hasFragment:${request.requestedUri.normalizePath().hasFragment}");
print("_server requestedUri normalizePath fragment:${request.requestedUri.normalizePath().fragment}");
print("_server uri:${request.uri}");
print("_server hasFragment:${request.uri.hasFragment}");
print("_server Fragment:${request.uri.fragment}");
print("_server parm count:${request.uri.queryParameters.length}");
for (int i = 0; i <request.uri.queryParameters.length; i++ ) {
print("_server parm:${request.uri.queryParameters[i]}");
}
final String token = request.uri.fragment;
request.response
..statusCode = 200
..headers.set("Content-Type", ContentType.html.mimeType)
..write("<html><h1>Notice the token in the URL fragment above</h1></html>");
await request.response.close();
await server.close(force: true);
onCode.add(token);
await onCode.close();
});
return onCode.stream;
}
以下是链接:
Kevin Segaud的博客文章:https://medium.com/@segaud.kevin/facebook-oauth-login-flow-with-flutter-9adb717c9f2e
Trello授权:https://developers.trello.com/page/authorization
以及仿真器的图片: