1

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 :)

Michael Knight
  • 39
  • 2
  • 11

1 Answers1

0

I hope you can interpret some code :)

This guy here made a perfect working Google-Login example.

The Google People Api is for the users contacts, so you would need this & this package.

Now need both google_sign_in with firebase_auth?

yes you then link the google sign in with an firebase_auth user through:

await authInstance.signInWithCredential(credential);
Marc Ma
  • 318
  • 2
  • 12
  • Hi Marc! Thanks for answering, I see that I can get the profile and some of the user's data, but Do I then do a get request to the People API with the uid I got or do I utilize the googleapis plugin somehow? Also do I need to register a new app in the API console? I have one registered but I didn't know what the links for the authorized domains were. Thank You! – Michael Knight Nov 01 '19 at 09:58
  • The reason I'm asking about registering the app is because the googleapis plugin needs the api credentials final _credentials = new ServiceAccountCredentials.fromJson(r''' { "private_key_id": ..., "private_key": ..., "client_email": ..., "client_id": ..., "type": "service_account" } '''); – Michael Knight Nov 01 '19 at 10:39