0

I'm trying to implement google_sign_in with my current project. Here is how I'm using the signin method:

 final GoogleSignIn _googleSignIn = GoogleSignIn(
    scopes: [
      'email',
      'profile',
    ],
  );

  String? _accessToken;
  String? _refreshToken;
  GoogleSignInAccount? _user;

  String? get token => _accessToken;

  bool get isAuthenticated => _accessToken != null;

  Future<void> signInWithGoogle() async {
    try {
      final account = await _googleSignIn.signIn();
      final authentication = await account?.authentication;
      final idToken = authentication?.idToken;
      if (idToken != null) {
        final response = await http.post(
          Uri.parse('$baseAuthUrl/oauth2/google'),
          headers: {'Content-Type': 'application/json'},
          body: jsonEncode({'idToken': idToken}),
        );
        if (response.statusCode == 200) {
      
        } else {
          print('Error: ${response.body}');
        }
      }
    } catch (e, stack) {
      print('Google Sign-In error: $e');
      print('Stacktrace: $stack');
    }
  }

I've created the OAuth 2.0 Client ID with the right fingerprint and the package name. When I call the signInWithGoogle method my credentials will be printed, but the idToken is always null.

This is a part of the app/build.gradle file:

  defaultConfig {
        applicationId "com.example.ranger"
        minSdkVersion 21
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

I've also added the package to my AndroidManifest, because it was not in it

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.ranger">
    <uses-permission android:name="android.permission.INTERNET"/>
</manifest>

But its still not working. What is missing here? Are there any extra settings?

1 Answer 1

0

Yes, add extra settings:

And set client ID.

final _googleSignIn = GoogleSignIn(scopes: ['email', 'profile', 'openid'], clientId: clientId);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.