在 google api(日历)上使用 flutter 进行身份验证
Authentication on google api (calendar) with flutter
我尝试使用
从 google 日历 api 中检索事件
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": "xxx@xxx.iam.gserviceaccount.com",
"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();
}
clientViaServiceAccount 行抛出异常
FormatException (FormatException: Unexpected end of input)
凭据应该没问题,因为我已经在不同的客户端中使用过它们。在 fiddler 中没有传出流量,因此看起来甚至在请求发送之前就出现了错误。代码有什么问题?
编辑
这是错误的堆栈
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════ The following
FormatException was thrown running a test: Unexpected end of input
抛出异常时,这是堆栈:
#0 _ChunkedJsonParser.fail (飞镖:convert-patch/convert_patch.飞镖:1392:5)
#1 _ChunkedJsonParser.close (飞镖:convert-patch/convert_patch.飞镖:510:7)
#2 _JsonStringDecoderSink.close (飞镖:convert-patch/convert_patch.飞镖:1487:13)
#3 _ConverterStreamEventSink.close(飞镖:convert/chunked_conversion.飞镖:80:18)
#15 _StringAdapterSink.close(飞镖:convert/string_conversion.飞镖:249:11)
#16 _Utf8ConversionSink.close(飞镖:convert/string_conversion.飞镖:300:20)
#17 _ConverterStreamEventSink.close(飞镖:convert/chunked_conversion.飞镖:80:18)
#46AutomatedTestWidgetsFlutterBinding.pump。 (包:flutter_test/src/binding.dart:855:25)
#49 TestAsyncUtils.guard(包:flutter_test/src/test_async_utils.dart:69:41)
#50 AutomatedTestWidgetsFlutterBinding.pump(包:flutter_test/src/binding.dart:840:27)
#51WidgetTester.pumpWidget。 (包:flutter_test/src/widget_tester.dart:318:22)
#54 TestAsyncUtils.guard(包:flutter_test/src/test_async_utils.dart:69:41)
#55 WidgetTester.pumpWidget(包:flutter_test/src/widget_tester.dart:315:27)
#56 主要。 (文件:///C:/projekte/flutter_garden/flutter_garden/test/widget_test.dart:9:18)
#58 主要。 (文件:///C:/projekte/flutter_garden/flutter_garden/test/widget_test.dart:7:43)
#59 testWidgets..(包:flutter_test/src/widget_tester.dart:119:25)
#61 testWidgets..(包:flutter_test/src/widget_tester.dart:117:9)
#62 TestWidgetsFlutterBinding._runTestBody(包:flutter_test/src/binding.dart:648:19)
#76【=72=】。 (包:flutter_test/src/binding.dart:1032:17)
#78【=72=】。 (package:flutter_test/src/binding.dart:1020:35)(从中删除了 74 帧
class _FakeAsync、包 dart:async、包 dart:async-补丁,以及
包裹 stack_trace)
运行 通过模拟器而不是来自单元测试的代码修复了问题。
我尝试使用
从 google 日历 api 中检索事件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": "xxx@xxx.iam.gserviceaccount.com",
"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();
}
clientViaServiceAccount 行抛出异常
FormatException (FormatException: Unexpected end of input)
凭据应该没问题,因为我已经在不同的客户端中使用过它们。在 fiddler 中没有传出流量,因此看起来甚至在请求发送之前就出现了错误。代码有什么问题?
编辑
这是错误的堆栈
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════ The following FormatException was thrown running a test: Unexpected end of input
抛出异常时,这是堆栈: #0 _ChunkedJsonParser.fail (飞镖:convert-patch/convert_patch.飞镖:1392:5) #1 _ChunkedJsonParser.close (飞镖:convert-patch/convert_patch.飞镖:510:7) #2 _JsonStringDecoderSink.close (飞镖:convert-patch/convert_patch.飞镖:1487:13) #3 _ConverterStreamEventSink.close(飞镖:convert/chunked_conversion.飞镖:80:18) #15 _StringAdapterSink.close(飞镖:convert/string_conversion.飞镖:249:11) #16 _Utf8ConversionSink.close(飞镖:convert/string_conversion.飞镖:300:20) #17 _ConverterStreamEventSink.close(飞镖:convert/chunked_conversion.飞镖:80:18) #46AutomatedTestWidgetsFlutterBinding.pump。 (包:flutter_test/src/binding.dart:855:25) #49 TestAsyncUtils.guard(包:flutter_test/src/test_async_utils.dart:69:41) #50 AutomatedTestWidgetsFlutterBinding.pump(包:flutter_test/src/binding.dart:840:27) #51WidgetTester.pumpWidget。 (包:flutter_test/src/widget_tester.dart:318:22) #54 TestAsyncUtils.guard(包:flutter_test/src/test_async_utils.dart:69:41) #55 WidgetTester.pumpWidget(包:flutter_test/src/widget_tester.dart:315:27) #56 主要。 (文件:///C:/projekte/flutter_garden/flutter_garden/test/widget_test.dart:9:18) #58 主要。 (文件:///C:/projekte/flutter_garden/flutter_garden/test/widget_test.dart:7:43) #59 testWidgets..(包:flutter_test/src/widget_tester.dart:119:25) #61 testWidgets..(包:flutter_test/src/widget_tester.dart:117:9) #62 TestWidgetsFlutterBinding._runTestBody(包:flutter_test/src/binding.dart:648:19) #76【=72=】。 (包:flutter_test/src/binding.dart:1032:17) #78【=72=】。 (package:flutter_test/src/binding.dart:1020:35)(从中删除了 74 帧 class _FakeAsync、包 dart:async、包 dart:async-补丁,以及 包裹 stack_trace)
运行 通过模拟器而不是来自单元测试的代码修复了问题。