谷歌日历API获取'无法读取属性'signIn'为null'

时间:2017-02-07 20:53:12

标签: javascript calendar google-api google-calendar-api

我正在尝试使用Google的Calendar API来获取特定日历的信息。我一直收到此错误Cannot read property 'signIn' of null。我在这里使用这个例子https://developers.google.com/google-apps/calendar/quickstart/js

知道为什么会这样吗?有人能给我一个更简单的方法来获取日历数据吗?基本上我想知道我的日历Feed中哪些房间可以免费预订。我可以使用日历API执行此操作吗?

const CLIENT_ID = '418414126140-u5u3r2ampke8egh7d2tk50ferkl8ri26.apps.googleusercontent.com';
const SCOPES = ["https://www.googleapis.com/auth/calendar.readonly"];

app.factory('getCalendar', ['$q', function($q){
    return function checkAuth() {
        var deferred = $q.defer();

        function handleClientLoad() {
            gapi.load('client:auth2', initClient);
        }
        handleClientLoad();

        function initClient() {
            gapi.client.init({
              "client_id": CLIENT_ID,
              "scope": SCOPES
            }).then(function () {
                gapi.auth2.getAuthInstance().signIn();
            });
        }

        function updateSigninStatus(isSignedIn) {
            if (isSignedIn) {
                console.log("You are authorized");
                listUpcomingEvents();
            } else {
                console.log("You are not authorized");
            }
        }

        function listUpcomingEvents() {
            gapi.client.calendar.events.list({
                'calendarId': 'primary',
                'timeMin': (new Date()).toISOString(),
                'showDeleted': false,
                'singleEvents': true,
                'maxResults': 10,
                'orderBy': 'startTime'
            }).then(function(response) {
                console.log(response);
                deferred.resolve(response);

            });
        }
        return deferred.promise;

    } //return ends here

}]);

1 个答案:

答案 0 :(得分:-1)

尝试使用gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);代替gapi.auth2.getAuthInstance().signIn();。您可以参考此示例code,了解如何使用Google JavaScript客户端API库对API进行身份验证和交互。

同时检查此related SO thread并查看是否有帮助。

<强>更新

如何检查日历上哪些房间可用?

您可以查看以下链接:Google Calendar Api, Is meeting Room available?

  

您需要以具有房间读取权限的用户身份进行身份验证。然后,您可以获取Calendar Resource API的resourceEmail字段,并将其用作events.list() Calendar API调用中的calendarId。