Mathias F
Mathias F

Reputation: 15891

Authentication on google api (calendar) with flutter

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)

Upvotes: 2

Views: 1489

Answers (1)

Mathias F
Mathias F

Reputation: 15891

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

Upvotes: 1

Related Questions