'auth2 api google calendar flutter web with flutter_web_auth
I am trying to implement the google calendar api with flutter web, I have been looking for several ways to make the connection and the only one that looks promising is using flutter_web_auth, I followed the corresponding documentation and I got it to redirect to the page to put the email and password and I put as it says in the documentation a page for the redirect, but it does not bring me any accessToken, I do not know what I'm doing wrong, this is my code:
final googleClientId = 'xxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com';
final callbackUrlScheme = 'https://xxxxx.com/xxx/callback';
// Construct the url
final url = Uri.https('accounts.google.com', '/o/oauth2/v2/auth', {
'response_type': 'code',
'client_id': googleClientId,
'redirect_uri': '$callbackUrlScheme',
'scope': "https://www.googleapis.com/auth/calendar",
});
// Present the dialog to the user
final result = await FlutterWebAuth.authenticate(url: url.toString(), callbackUrlScheme: callbackUrlScheme);
// Extract code from resulting url
final code = Uri.parse(result).queryParameters['code'];
final url2 = Uri.https('googleapis.com', '/oauth2/v4/token', {
'client_id': googleClientId,
'redirect_uri': '$callbackUrlScheme',
'grant_type': 'authorization_code',
'code': code,
});
// Use this code to get an access token
final response = await http.post(url2);
// Get the access token from the response
final accessToken = jsonDecode(response.body)['access_token'] as String;
print(accessToken);
in the callback.html page I have
<!DOCTYPE html>
<title>Authentication complete</title>
<p>Authentication is complete. If this does not happen automatically, please
close the window.
<script>
window.opener.postMessage({
'flutter-web-auth': window.location.href
}, window.location.origin);
window.close();
</script>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
