Skip to content

Instantly share code, notes, and snippets.

@cshannon3
Last active October 20, 2018 04:36
Show Gist options
  • Save cshannon3/ee4ee9b1e58f17f4ed1d97e603914f45 to your computer and use it in GitHub Desktop.
Save cshannon3/ee4ee9b1e58f17f4ed1d97e603914f45 to your computer and use it in GitHub Desktop.
Flutter InApp Authentication
import 'dart:async';
import 'package:flutter/material.dart';
import "package:http/http.dart" as http;
import "package:googleapis_auth/auth_io.dart" as auth;
import 'package:googleapis/calendar/v3.dart' as calendar;
import 'package:google_sign_in/google_sign_in.dart';
final GoogleSignIn _googleSignIn =
new GoogleSignIn(scopes: [calendar.CalendarApi.CalendarScope]);
final scopes = [calendar.CalendarApi.CalendarScope];
...
...
class _MyHomePageState extends State<MyHomePage> {
calendar.CalendarApi calendarApi;
_testSignInWithGoogle() async {
final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
final GoogleSignInAuthentication googleAuth =
await googleUser.authentication;
auth.AccessToken token = auth.AccessToken("Bearer", googleAuth.accessToken,
DateTime.now().add(Duration(days: 1)).toUtc());
auth.AccessCredentials(token, googleUser.id, scopes);
http.BaseClient _client = http.Client();
auth.AuthClient _authClient = auth.authenticatedClient(
_client, auth.AccessCredentials(token, googleUser.id, scopes));
calendarApi = new calendar.CalendarApi(_authClient);
}
@override
void initState() {
super.initState();
_testSignInWithGoogle();
}
...
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment