I'm trying to access the Google People API to get some user data such as name, email, and so on. I used the google_sign_in plugin and firebase_auth to log the user in via the Google Sign-in and I would then like to get his data mentioned up there and simply save it to my database. The problem is that nothing what I tried seems to work and I can't really find much more about this. Also is it true that You now have to use google_sign_in with firebase_auth and can't just use it without Firebase? Because I only need to userData from the People API.
Some "solutions" I found on stack aren't working, one of them beeing How to use Google API in flutter? I have also tried using this code https://github.com/flutter/plugins/blob/master/packages/google_sign_in/example/lib/main.dart but it's without Firebase and I've seen people say that You now have to use it together with Firebase.
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:googleapis/people/v1.dart';
import 'package:http/http.dart'
show BaseRequest, IOClient, Response, StreamedResponse, get;
import 'package:http/io_client.dart';
import 'package:google_sign_in/google_sign_in.dart'
show GoogleSignIn, GoogleSignInAccount;
import 'package:googleapis/people/v1.dart'
show ListConnectionsResponse, PeopleApi;
final FirebaseAuth _auth = FirebaseAuth.instance;
final GoogleSignIn googleSignIn = GoogleSignIn(
scopes: <String>['https://www.googleapis.com/auth/userinfo.profile','https://www.googleapis.com/auth/userinfo.email'],
);
Future<String> signInWithGoogle() async {
final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
final GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;
final authHeaders = googleSignIn.currentUser.authHeaders;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
final AuthResult authResult = await _auth.signInWithCredential(credential);
final FirebaseUser user = authResult.user;
assert(!user.isAnonymous);
assert(await user.getIdToken() != null);
final FirebaseUser currentUser = await _auth.currentUser();
assert(user.uid == currentUser.uid);
return 'signInWithGoogle succeeded: $user';
}
void signOutGoogle() async{
await googleSignIn.signOut();
print("User Sign Out");
}
class GoogleHttpClient extends IOClient {
Map<String, String> _headers;
GoogleHttpClient(this._headers) : super();
@override
Future<StreamedResponse> send(BaseRequest request) =>
super.send(request..headers.addAll(_headers));
@override
Future<Response> head(Object url, {Map<String, String> headers}) =>
super.head(url, headers: headers..addAll(_headers));
}
I except to simply display a Sign In with Google dialog, the user clicks it and I take his data from the People API and send it to my backend and create a user instance from his data. That's basically it :)