Home > Software engineering >  Unhandled Exception: PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiExceptio
Unhandled Exception: PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiExceptio

Time:10-10

I'm trying to make google login with flutter app, and I got this error. I've already get and put SHA-1 key to OAuth and Firebase and set google-services.json file under app directory. But I still got same problem.

 E/flutter (12401): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 10: , null, null)
E/flutter (12401): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
E/flutter (12401): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:156:18)
E/flutter (12401): <asynchronous suspension>
E/flutter (12401): #2      MethodChannel.invokeMapMethod (package:flutter/src/services/platform_channel.dart:356:43)
E/flutter (12401): <asynchronous suspension>
E/flutter (12401): #3      GoogleSignIn._callMethod (package:google_sign_in/google_sign_in.dart:235:30)
E/flutter (12401): <asynchronous suspension>
E/flutter (12401): #4      GoogleSignIn.signIn.isCanceled (package:google_sign_in/google_sign_in.dart)
E/flutter (12401): <asynchronous suspension>
E/flutter (12401): 

This is how I set.

GCP OAuth

Firebase setting - I filled all blanks

and I don't know which clientId has to be on this area.

which clientId has to be on this area?

I got 4 clientId because I already used google clientId to make django web login with google

CodePudding user response:

Step 1 : Add this dependency into your pubsec file

firebase: ^9.0.2
  firebase_core: ^1.2.1
  firebase_database: ^7.1.0
  firebase_auth: ^1.2.0
  font_awesome_flutter: ^9.0.0
  google_sign_in: ^5.0.3

Step : 2 Create a new dart file and paste this code

import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';

class FirebaseService {
  final FirebaseAuth _auth = FirebaseAuth.instance;
  final GoogleSignIn _googleSignIn = GoogleSignIn();

  Future<String?> signInwithGoogle() async {
    try {
      final GoogleSignInAccount? googleSignInAccount =
          await _googleSignIn.signIn();
      final GoogleSignInAuthentication googleSignInAuthentication =
          await googleSignInAccount!.authentication;
      final AuthCredential credential = GoogleAuthProvider.credential(
        accessToken: googleSignInAuthentication.accessToken,
        idToken: googleSignInAuthentication.idToken,
      );
      await _auth.signInWithCredential(credential);
    } on FirebaseAuthException catch (e) {
      print(e.message);
      throw e;
    }
  }

  Future<void> signOutFromGoogle() async{
    await _googleSignIn.signOut();
    await _auth.signOut();
  }
}

Step 3: call sign in with google function:

Enable Google sign in Firebase Add your SHA 1 key into Firebase Project

CodePudding user response:

I pasted your code but I got same issue

    E/flutter (11108): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 10: , null, null)
E/flutter (11108): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
E/flutter (11108): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:156:18)
E/flutter (11108): <asynchronous suspension>
E/flutter (11108): #2      MethodChannel.invokeMapMethod (package:flutter/src/services/platform_channel.dart:356:43)
E/flutter (11108): <asynchronous suspension>
E/flutter (11108): #3      GoogleSignIn._callMethod (package:google_sign_in/google_sign_in.dart:235:30)
E/flutter (11108): <asynchronous suspension>
E/flutter (11108): #4      GoogleSignIn.signIn.isCanceled (package:google_sign_in/google_sign_in.dart)
E/flutter (11108): <asynchronous suspension>
E/flutter (11108): 

This is my code

import 'dart:io';
import 'dart:async';
import 'dart:convert' show json;
import 'package:firebase_auth/firebase_auth.dart';
import "package:http/http.dart" as http;

import 'package:billim/base/home_page.dart';
import 'package:billim/user/login_result.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

// 카카오 로그인
import 'package:kakao_flutter_sdk/auth.dart';
import 'package:kakao_flutter_sdk/user.dart';
import 'package:kakao_flutter_sdk/common.dart';

// 네이버 로그인
import 'package:flutter/services.dart';
import 'package:flutter_naver_login/flutter_naver_login.dart';
import 'package:google_sign_in/google_sign_in.dart';

import 'package:webview_flutter/webview_flutter.dart';

final FirebaseAuth _auth = FirebaseAuth.instance;
final GoogleSignIn _googleSignIn = GoogleSignIn();

class LoginScreen extends StatefulWidget {
  LoginScreen({Key? key}) : super(key: key);

  @override
  _LoginScreenState createState() => _LoginScreenState();
}

class _LoginScreenState extends State<LoginScreen> {
  List<String> loginIcon = [
    'http://static.billim.co.kr/icons/login_icon_1.png',
    "http://static.billim.co.kr/icons/login_icon_2.png",
    "http://static.billim.co.kr/icons/login_icon_3.png",
  ];

......

Widget _buildBody() {

.......
    Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: List<Widget>.generate(loginIcon.length, (idx) {
          return Padding(
              padding: const EdgeInsets.symmetric(horizontal: 10.0),
                child: SizedBox(
                  width: 60.0,
                  height: 60.0,
                  child: IconButton(
                    onPressed: () {
                      if(idx == 0) {
                        buttonLoginPressed();
                      else if(idx == 1) {
                        _loginWithKakao();
                      } else if(idx == 2) {
                      signInwithGoogle();
                      }
                    },
                    icon: Image.network(loginIcon[idx], fit: BoxFit.fill,),
                  ),
                 ),
               );
              },)
            ),
......

Future<String?> signInwithGoogle() async {
    try {
      final GoogleSignInAccount? googleSignInAccount =
      await _googleSignIn.signIn();
      final GoogleSignInAuthentication googleSignInAuthentication =
      await googleSignInAccount!.authentication;
      final AuthCredential credential = GoogleAuthProvider.credential(
        accessToken: googleSignInAuthentication.accessToken,
        idToken: googleSignInAuthentication.idToken,
      );
      await _auth.signInWithCredential(credential);
    } on FirebaseAuthException catch (e) {
      print(e.message);
      throw e;
    }
  }

  Future<void> signOutFromGoogle() async{
    await _googleSignIn.signOut();
    await _auth.signOut();
  }
  • Related