Google Calendar API getting 'Cannot read property 'signIn' of null'

空扰寡人 提交于 2021-01-27 14:10:28

问题


I'm trying to use Google's Calendar API to get info on specific calendar. I keep getting this error Cannot read property 'signIn' of null. I'm using the example online here https://developers.google.com/google-apps/calendar/quickstart/js.

Any idea why this is happening? And can someone give me a more simple way to get calendar data? Basically I want to know which rooms in my calendar feed are free to book. Can I do this using the calendar 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:


Try using gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus); instead of gapi.auth2.getAuthInstance().signIn();. You may refer with this sample code on how to use the Google JavaScript Client API Library to authenticate and interact with the API.

Also check this related SO thread and see if it helps.

UPDATE:

Any idea on how to check which rooms on your calendar are available or not?

You may check on this link: Google Calendar Api, Is meeting Room available?

You will need to be authenticated as a user that has read-access to the room. Then you can take the resourceEmail field of the Calendar Resource API and use it as a calendarId in the events.list() Calendar API call.



来源:https://stackoverflow.com/questions/42099791/google-calendar-api-getting-cannot-read-property-signin-of-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!