首页 > 解决方案 > Flutter Google Calendar API - clientViaServiceAccount 没有响应

问题描述


我知道这个问题已经有了答案,但我无法解决我的问题,
所以我希望你能帮助我,或者至少建议我尝试什么,因为我不知道该怎么做或怎么做调试以解决我的问题。

我正在尝试使用Google Calendar Api列出公司日历中的所有事件。
我在这个问题上使用了相同的代码:Using dart and flutter with google calendar api to get a list of events on the a user's calendar
我遵循了上述问题中答案的所有6 个步骤

由于某种原因,该调用clientViaServiceAccount不返回任何结果和任何错误。
它似乎正在执行一个无限循环。

这是我正在使用的代码,我只能看到打印的“getCalendarEvents”。我看不到味精“这里”或打印的任何错误。所以这个问题是肯定的clientViaServiceAccount

我根据Iambichus的建议编辑了代码,但问题是我什至无法访问CalendarApi. 代码堆叠在clientViaServiceAccount.

import 'package:flutter/material.dart';
import 'package:googleapis_auth/auth_io.dart';
import 'package:googleapis/calendar/v3.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    CalendarAPI.getCalendarEvents();
    return MaterialApp(
      title: 'Flutter Demo',
      home: Container(),
    );
  }
}

class CalendarAPI {
  static final _accountCredentials = new ServiceAccountCredentials.fromJson(r'''
  {
    "private_key_id": myPrivatekeyId,
    "private_key": myPrivateKey,
    "client_email": myClientEmail,
    "client_id": myClientId,
    "type": "service_account"
  }
  ''');
  
  static final _scopes = [CalendarApi.CalendarScope];

  static void getCalendarEvents() {
    print('getCalendarEvents');
    clientViaServiceAccount(_accountCredentials, _scopes).then((client) {
      print('HERE');
      var calendar = new CalendarApi(client);
      print(calendar);

      // Added iamblichus answer
      var calendarListEntry = CalendarListEntry();
      calendarListEntry.id = calendarId;
      calendar.calendarList.insert(calendarListEntry).then((_) {
        print('CALENDAR ADDED');

        var calEvents = calendar.events.list(calendarId);
        calEvents.then((Events events) {
          events.items.forEach((Event event) {
            print(event.summary);
          });
        }).catchError((e) => print(e));
      }).catchError((e) => print(e));
    }).catchError((e) => print(e));
  }
}

编辑回答 iamblichus 评论 7 月 15 日 12:31

我在下图所示的页面上创建了服务帐户凭据。
一旦我创建了密钥,我就下载了带有凭据的.json文件。
我复制了该文件的内容并粘贴到ServiceAccountCredentials.fromJson函数中,因此凭据不会出​​错。
即使凭据错误,为什么我看不到clientViaServiceAccount呼叫失败的错误?
我发现任何错误并将它们与最后一个}).catchError((e) => print(e));.
由于某种原因,电话clientViaServiceAccount没有做任何事情,我不明白如何找到原因。 在此处输入图像描述

标签: flutterdartgoogle-apigoogle-calendar-api

解决方案


此方法适用于云端。如果你想在 Flutter 应用程序中使用它,你必须使用以下插件让用户进行身份验证,

extension_google_sign_in_as_googleapis_auth

推荐阅读