using the googleapis library in dart to update a c

2019-02-28 22:22发布

问题:

I am new to dart and I have been trying to figure out how to use the googleapis library to update a calendars events, then display the calendar/events on a webpage.

So far I have this code that I was hoping would just change the #text id's text to a list of events from the selected calendars ID:

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

final _credentials = new ServiceAccountCredentials.fromJson(r'''
{
  "private_key_id": "myprivatekeyid",
  "private_key": "myprivatekey",
  "client_email": "myclientemail",
  "client_id": "myclientid",
  "type": "service_account"
}
''');

const _SCOPES = const [CalendarApi.CalendarScope];

void main() {
  clientViaServiceAccount(_credentials, _SCOPES).then((http_client) {
    var calendar = new CalendarApi(http_client);

    String adminPanelCalendarId = 'mycalendarID';

    var event = calendar.events;

    var events = event.list(adminPanelCalendarId);

    events.then((showEvents) {
      querySelector("#text2").text = showEvents.toString();
    });

  });
}

But nothing displays on the webpage. I think I am misunderstanding how to use client-side and server-side code in dart... Do I break up the file into multiple files? How would I go about updating a calendar and displaying it on a web page with dart?

I'm familiar with the browser package, but this is the first time I have written anything with server-side libraries(googleapis uses dart:io so I assume it's server-side? I cannot run the code in dartium).

If anybody could point me in the right direction, or provide an example as to how this could be accomplished, I would really appreciate it!

回答1:

Using the following code you can display the events of a calendar associated with the logged account. In this example i used createImplicitBrowserFlow ( see the documentation at https://pub.dartlang.org/packages/googleapis_auth ) with id and key from Google Cloud Console Project.

import 'dart:html';
import 'package:googleapis/calendar/v3.dart';
import 'package:googleapis_auth/auth_browser.dart' as auth;

var id = new auth.ClientId("<yourID>", "<yourKey>");
var scopes = [CalendarApi.CalendarScope];

  void main() {

  auth.createImplicitBrowserFlow(id, scopes).then((auth.BrowserOAuth2Flow flow) {
        flow.clientViaUserConsent().then((auth.AuthClient client) {

          var calendar = new CalendarApi(client);

              String adminPanelCalendarId = 'primary';

              var event = calendar.events;

              var events = event.list(adminPanelCalendarId);

              events.then((showEvents) {
                showEvents.items.forEach((Event ev) { print(ev.summary); });
                querySelector("#text2").text = showEvents.toString();
              });      


          client.close();
          flow.close();
        });
      });

}


回答2:

What you might be looking for is the hybrid flow. This produces two items

  • access credentials (for client side API access)
  • authorization code (for server side API access using the user credentials)

From the documentation:

Use case: A web application might want to get consent for accessing data on behalf of a user. The client part is a dynamic webapp which wants to open a popup which asks the user for consent. The webapp might want to use the credentials to make API calls, but the server may want to have offline access to user data as well.

The page Google+ Sign-In for server-side apps describes how this flow works.