我正在尝试通过重新构建以前用Java编写的应用程序来学习如何使用飞镖和颤动,这涉及到使用Google自己的Calendar API从Google Calendar获取事件。
通过阅读(不是很详细的)有关googleapis_auth包的文档,以及StackOverflow上唯一的一个非常类似的问题的线程,我已经设法将理论上可行的代码组合在一起:
import 'package:googleapis/calendar/v3.dart';
import 'package:googleapis_auth/auth_io.dart';
//get Service Account Credentials
final accountCredentials = new ServiceAccountCredentials.fromJson({
"private_key_id": "myprivatekeyid",
"private_key": "myprivatekey",
"client_email": "myclientemail",
"client_id": "myclientid",
"type": "service_account"
});
var _scopes = [CalendarApi.CalendarScope]; //defines the scopes for the calendar api
void getCalendarEvents() {
clientViaServiceAccount(accountCredentials, _scopes).then((client) {
var calendar = new CalendarApi(client);
var calEvents = calendar.events.list("primary");
calEvents.then((Events events) {
events.items.forEach((Event event) {print(event.summary);});
});
});
}
Run Code Online (Sandbox Code Playgroud)
上面的代码在模拟器上运行时不会产生任何错误,也不会在控制台中打印任何事件摘要。但是,当在项目的仪表板上查看时,请求将以成功响应200响应。我还尝试过使用嵌套在clientViaServiceAccount中的类似代码来获取我所有日历的ID,并且它也不会返回任何内容。
另外,从文档中,我发现访问API的最简单方法是通过客户端服务帐户(如代码所示),而不是通过OAuth2客户端ID(我更习惯了)。
我想念什么吗?代码是否错误?也许我需要弄乱服务帐户上的设置?任何帮助,将不胜感激。