2017-02-07 99 views
3

我试图使用Google的Calendar API来获取特定日历上的信息。我不断收到此错误Cannot read property 'signIn' of null。我在这里使用在线示例https://developers.google.com/google-apps/calendar/quickstart/jsGoogle Calendar API获取'无法读取属性'登录'null'

任何想法为什么会发生这种情况?有人可以给我一个更简单的方法来获取日历数据?基本上我想知道我的日历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

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

也检查此related SO thread,看看是否有帮助。

UPDATE:

如何检查哪些日历上的房间用或不知道吗?

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

您将需要被认证为已读取进入房间的用户。然后,您可以采用日历资源API的resourceEmail字段,并将其用作日历API调用events.list()中的calendarId。

+0

谢谢。我发现这个在线教程比谷歌的文档更容易使用。 https://www.youtube.com/watch?v=0rZmp3Ew_kA 有关如何检查日历上的哪些房间可用或不可用的任何想法? –