Authentication on google api (calendar) with flutter

684

Running the code via the emulator and not from the unittests fixed the problem.

Share:
684
Mathias F
Author by

Mathias F

Guy from Germany who loves brooklynisms. Do they realy use the term Coney Island Whitefish?

Updated on December 21, 2022

Comments

  • Mathias F
    Mathias F over 1 year

    I try to retrieve events from google calendar api using

    import 'package:googleapis_auth/auth_io.dart';
    import 'package:http/http.dart' as http;
    import 'package:googleapis/calendar/v3.dart' as calendarapi;
    

    ...

    class _MyHomePageState extends State<MyHomePage> {
    
      final accountCredentials = new ServiceAccountCredentials.fromJson(
        {
        "private_key_id": "562ab...",
        "private_key": "-----BEGIN PRIVATE KEY--............----END PRIVATE KEY-----\n",
        "client_email": "[email protected]",
        "client_id": "1073.......",
        "type": "service_account",
        "project_id": "myapi"
       }
      );
    
      final scopes = [calendarapi.CalendarApi.CalendarScope];
      final client = new http.Client();
     
    
      void getCalendarEvents() { 
          clientViaServiceAccount(accountCredentials, scopes).then((client) {
            var calendar = new calendarapi.CalendarApi(client);
            var calEvents = calendar.events.list("primary");
            calEvents.then((calendarapi.Events events) {
              events.items.forEach((calendarapi.Event event) {print(event.summary);});
            });
              client.close();
          });
      }
    }
    

    ...

      @override
      void initState() {
        super.initState();
        getCalendarEvents();
      }
    

    The line clientViaServiceAccount throws an exception

    FormatException (FormatException: Unexpected end of input)

    The credentials should be fine because I already use them in a different client. In fiddler there is no outgoing traffic so it looks like there is an error even before the request gets send. Whats wrong in the code?

    EDIT

    This is the stack of the error

    ══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════ The following FormatException was thrown running a test: Unexpected end of input

    When the exception was thrown, this was the stack: #0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1392:5) #1 _ChunkedJsonParser.close (dart:convert-patch/convert_patch.dart:510:7) #2 _JsonStringDecoderSink.close (dart:convert-patch/convert_patch.dart:1487:13) #3 _ConverterStreamEventSink.close (dart:convert/chunked_conversion.dart:80:18) #15 _StringAdapterSink.close (dart:convert/string_conversion.dart:249:11) #16 _Utf8ConversionSink.close (dart:convert/string_conversion.dart:300:20) #17 _ConverterStreamEventSink.close (dart:convert/chunked_conversion.dart:80:18) #46 AutomatedTestWidgetsFlutterBinding.pump. (package:flutter_test/src/binding.dart:855:25) #49 TestAsyncUtils.guard (package:flutter_test/src/test_async_utils.dart:69:41) #50 AutomatedTestWidgetsFlutterBinding.pump (package:flutter_test/src/binding.dart:840:27) #51 WidgetTester.pumpWidget. (package:flutter_test/src/widget_tester.dart:318:22) #54 TestAsyncUtils.guard (package:flutter_test/src/test_async_utils.dart:69:41) #55 WidgetTester.pumpWidget (package:flutter_test/src/widget_tester.dart:315:27) #56 main. (file:///C:/projekte/flutter_garden/flutter_garden/test/widget_test.dart:9:18) #58 main. (file:///C:/projekte/flutter_garden/flutter_garden/test/widget_test.dart:7:43) #59 testWidgets.. (package:flutter_test/src/widget_tester.dart:119:25) #61 testWidgets.. (package:flutter_test/src/widget_tester.dart:117:9) #62 TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:648:19) #76 AutomatedTestWidgetsFlutterBinding.runTest. (package:flutter_test/src/binding.dart:1032:17) #78 AutomatedTestWidgetsFlutterBinding.runTest. (package:flutter_test/src/binding.dart:1020:35) (elided 74 frames from class _FakeAsync, package dart:async, package dart:async-patch, and package stack_trace)

    • Raserhin
      Raserhin almost 4 years
      I don't know if you copied the whole code but what I could see right away is that there is no closing bracket for the class ` _MyHomePageState, you might want to check on that. If I understood correctly your code is not even executing? Or is the error part of the client getting to the google server. I would say that you already did but if not read [the following page](https://github.com/dart-lang/googleapis_auth), in there is recommended to close` the client after the interaction.
    • Mathias F
      Mathias F almost 4 years
      I updated the code. client.close() did not change the outcome. getCalendarEvents is called within initState
    • Raserhin
      Raserhin almost 4 years
      But is your code even executing/compiling? Do you have a way to check if you are making the request to google servers?
    • Mathias F
      Mathias F almost 4 years
      Yes I run the code via a flutter unittest an it is hitting getCalendarEvents -> clientViaServiceAccount