'flutter - Waiting for getLinksSteam before continue oauth2
My problem is that my app won't wait for getLinksStream to be ready. In my case i want to login with OAuth2 and i need the code=xxxxxxxxxx responses after logging in. I'm need to get these responses with the command getUriLinksStream() after the Browser is closed, but After pressing the button for the first time it shows null. and return me this error
[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: FormatException: Invalid OAuth response for "https://xxxxxxxxx/oauth2/authorize": did not contain required parameter "code".
Thank you for help
final authorizationEndpoint =
Uri.parse("https:/XXXXXX/authorize");
final tokenEndpoint =
Uri.parse("XXXXXX/oauth2/token");
final identifier = 'XXXXXXXXXXXXXXX';
final secret = 'XXXXXXXXXXXXXXX';
final redirectUrl = Uri.parse('XXXXXX/spid_auth');
final credentialsFile = File('~/.myapp/credentials.json');
Future<oauth2.Client> createClient() async {
var exists = await credentialsFile.exists();
if (exists) {
var credentials =
oauth2.Credentials.fromJson(await credentialsFile.readAsString());
return oauth2.Client(credentials, identifier: identifier, secret: secret);
}
var grant = oauth2.AuthorizationCodeGrant(
identifier, authorizationEndpoint, tokenEndpoint,
secret: secret);
var authorizationUrl = grant.getAuthorizationUrl(redirectUrl);
await redirect(authorizationUrl);
var responseUrl = await listen(redirectUrl);
print(responseUrl);
return await grant.handleAuthorizationResponse(responseUrl.queryParameters);
}
void main() async {
var client = await createClient();
await credentialsFile.writeAsString(client.credentials.toJson());
}
Future<void> redirect(Uri url) async {
if (await canLaunch(url.toString())) {
await launch(url.toString());
print('redirect' + url.toString());
}
}
Future<Uri> listen(Uri url) async {
var responseUrl;
print('inside listen');
print(url);
/* if (await canLaunch(url.toString())) {
await launch(url.toString());
print('redirect' + url.toString());
}*/
// ignore: deprecated_member_use
getLinksStream().listen((String? uri) async { <--- the stopped here
print(uri);
}, onError: (err) {
print(err);
});
return Uri();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
