如何使用Google Calendar API针对角度项目从公共日历中获取事件?

时间:2019-02-04 09:48:34

标签: angular google-api google-calendar-api

我正在尝试使用Google日历中的公共假期填充日历。我已经使用Google Developer Console建立了一个项目,生成了API密钥和客户端ID。我也有日历ID,我需要从日历中获取事件。

当前,我收到的错误消息是-无法解析gapi.auth2 。这是解决这个问题的正确方法吗?

除了gapi之外,我尝试使用ng-gapi,但发现很难缠住它。我可以使用它来获取所有日历事件吗?您可能会说,我对这个框架是超级新手,茫然无措。任何帮助表示赞赏。

gcal.service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import "gapi.auth2"

@Injectable({
  providedIn: 'root'
})
export class GcalService {
  apiKey: string = 'key';
  clientId: string = 'clientid.apps.googleusercontent.com';
  scope: string = 'https://www.googleapis.com/auth/calendar.events';
  calendarId: string = 'en.malaysia#holiday@group.v.calendar.google.com';
  path: string = 'https://www.googleapis.com/calendar/v3/calendars/' + this.calendarId + '/events';
  events: any;
  error: any;
  getEvents() {
     gapi.client.init({
      'apiKey': this.apiKey,
      'clientId': this.clientId,
      'scope': this.scope
    }).then(function() {
      return gapi.client.request({
        'path': this.path
      })
    }).then(function(response) {
      console.log(response.result);
    }, function(reason) {
      console.log('Error: ' + reason.result.error.message);
    });
  }
  constructor(private http: HttpClient) { }
}

tsconfig.app.json

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "types": ["gapi.auth2"]
  },
  "exclude": [
    "test.ts",
    "**/*.spec.ts"
  ]
}

0 个答案:

没有答案
相关问题