2015-08-17 81 views
4

我正在嘗試使用NodeJS API從Google日曆獲取忙碌信息,如https://developers.google.com/google-apps/calendar/v3/reference/freebusy/query所列。問題是我得到的唯一錯誤響應是'無效請求'。當我使用Google的Try It工具運行它時,我能夠得到迴應。我知道我正在獲得授權客戶。我能做些什麼來進一步解決這個問題?如何解決freebusy google日曆nodejs api問題?

function getAvail(auth, dateTimeRange, calID) { 
    var deferred = Q.defer(); // get a new deferral 
    calendar.freebusy.query({ 
      auth: auth, 
      items: [{id: calID}], 
      timeMin: (dateTimeRange.start).toISOString(), 
      timeMax: (dateTimeRange.end).toISOString(), 
      }, function(err, response) { 
        console.log('Response from the Calendar service: ' + response); 
        if (err) { 
          console.log('There was an error contacting the Calendar service: ' + err); 
          deferred.reject(); // deferred reject here 
          return; 
        } 
        var events = response[calID]['busy']; 
        if (events.length == 0) { 
          console.log('No upcoming events found.'); 
        } else { 
          console.log('busy in here...'); 
        } 
        deferred.resolve(response); // deferred resolve here 
      }); 
    return deferred.promise; // return a promise 

}

回答

8

唷!我終於明白我做錯了什麼。這裏是代碼,與我犯了一個錯誤的評論。我希望有更好的方法來找出這個問題。

function getAvail(auth, dateTimeRange, calID) { 
    console.log('auth:'+JSON.stringify(auth)); 
    console.log('date Time Range :'+(dateTimeRange.start).toISOString()+' --->'+(dateTimeRange.end).toISOString()); 
    console.log('calendar id to check freebusy:'+calID); 
    var deferred = Q.defer(); // get a new deferral 
    calendar.freebusy.query({ 
      auth: auth, 
      headers: { "content-type" : "application/json" }, 
      resource:{items: [{"id" : calID}], //needed to include resource instead of sending the params directly. 

         timeMin: (dateTimeRange.start).toISOString(), 
         timeMax: (dateTimeRange.end).toISOString() 
        } 
    }, function(err, response) { 
        console.log('Response from the Calendar service: ' + JSON.stringify(response)); 
        if (err) { 
          console.log('There was an error contacting the Calendar service: ' + err); 
          deferred.reject(); // deferred reject here 
          return; 
        } 
        var events = response.calendars[calID].busy; 
        if (events.length == 0) { 
          console.log('No upcoming events found.'); 
        } else { 
          console.log('busy in here...'); 
        } 
        deferred.resolve(response); // deferred resolve here 
      }); 
    return deferred.promise; // return a promise 
} 

這是很多冥想和密切注視文件。 Google的nodejs庫在示例中沒有特別詳細記錄(當然,它仍然是alpha)。但這裏是freebusy功能: this.freebusy = {

/** 
* calendar.freebusy.query 
* 
* @desc Returns free/busy information for a set of calendars. 
* 
* @alias calendar.freebusy.query 
* @memberOf! calendar(v3) 
* 
* @param {object} params - Parameters for request 
* @param {object} params.resource - Request body data** <<<< -- this is what I was missing. 
* @param {callback} callback - The callback that handles the response. 
* @return {object} Request object 
*/ 
query: function(params, callback) { 
    var parameters = { 
    options: { 
     url: 'https://www.googleapis.com/calendar/v3/freeBusy', 
     method: 'POST' 
    }, 
    params: params, 
    requiredParams: [], 
    pathParams: [], 
    context: self 
    }; 

    return createAPIRequest(parameters, callback); 
} 

}; 

大約有內循環,我從這裏學到SO創建關閉一些額外獲悉:JavaScript closure inside loops – simple practical example。讓我知道是否有人遇到類似的問題。我會很樂意分享我的代碼示例。

+0

我一直在努力通過這個自己。感謝帖子,這幫助我解決問題! – Jeffpowrs