oauthLogin method Null safety
Login into Wialon using username and password through OAuth, this method will return an Access Token
Implementation
Future<String?> oauthLogin({required String username, required String password}) async {
String urlToGetSign = "https://hosting.wialon.com/login.html?"
"client_id=$username"
"&access_type=-1"
"&activation_time=0"
"&duration=0"
"&flags=0x1";
Uri uri = Uri.parse(urlToGetSign);
http.Response response = await http.get(uri);
Document page = parser.parse(response.body);
List<Element> elements = page.getElementsByTagName("input");
String? sign;
for (Element element in elements) {
if (element.attributes["name"] == "sign") {
sign = element.attributes["value"];
break;
}
}
if (sign == null) {
throw SdkException(message: "Sign not found");
}
Map<String, dynamic> request = {
'response_type': 'token',
'wialon_sdk_url': 'https://hst-api.wialon.com',
'client_id': username,
'access_type': '-1',
'activation_time': '0',
'duration': '0',
'flags': '7',
'login': username,
'passw': password,
'sign': sign,
'redirect_uri': 'https://hosting.wialon.com/post_token.html',
};
uri = Uri.parse("https://hosting.wialon.com/oauth.html");
response = await http.post(uri, body: request);
String redirect = response.headers['location']!;
uri = Uri.parse(redirect);
http.Client client = http.Client();
http.StreamedResponse response2 = await client.send(http.Request("GET", uri)..followRedirects = false);
String? responseUrl = response2.headers['location'];
if (responseUrl == null) {
throw SdkException(message: "Response url not found");
}
responseUrl = responseUrl.split("?").last;
Map<String, dynamic> responseMap = Uri.splitQueryString(responseUrl);
String? token = responseMap['access_token'];
return token;
}