1

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!

GioLaq
  • 2,489
  • 21
  • 26
RattleyCooper
  • 4,997
  • 5
  • 27
  • 43

2 Answers2

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.

sgjesse
  • 3,793
  • 14
  • 17
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();
        });
      });

}
GioLaq
  • 2,489
  • 21
  • 26
  • If I want to do this using a service account, will this code work? Also, would you be able to explain this a little bit? Like I said, I'm new to dart and I'm not exactly sure what is going on here :/ Thanks for the help either way JoaoBiriba. – RattleyCooper Dec 01 '14 at 20:40
  • what do you mean with service account? you can use with any account, because it has to login to authorize the client, in the documentation of googleapis_auth there are however other solutions. – GioLaq Dec 01 '14 at 21:11
  • The service account doesn't require user authentication. I want to set up my personal google calendar to display events that can be updated by my server. The users won't have to update their own calendars or add any events to my calendar. This is for displaying information that I'm pulling from a database. [More on service accounts](https://developers.google.com/accounts/docs/OAuth2ServiceAccount) – RattleyCooper Dec 01 '14 at 21:14
  • I see the problem, you have to develop a server to server application, so i think you have to split up in a project using auth_io.dart ( server ) and a client using data coming from it – GioLaq Dec 01 '14 at 21:51