使用批处理无法在Google日历上添加多个事件,因此无法托管

时间:2018-07-11 05:10:22

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

function sync()
{
	var boardId = $("#hiddenBoardId").val();
	$.ajax({
		  type: "POST",
		  url: "calendar/getAll",
		  data: {boardId:boardId},
		  dataType:"json",
		  success: function (response) {
			  	//console.log(response);
			  	var batch = gapi.client.newBatch();
			  	$.each(response, function(idx, res){
			  			var startDate = res.startDate;
			  			var dueDate = res.dueDate;
			  			var title = res.cardTitle;
			  			//alert(startDate); //2018-04-12 12:00:00
			  			var split = startDate.split(" ");
			  			var split2 = dueDate.split(" ");
			  			if(startDate != "" && dueDate != "" && dueDate > startDate)
			  			{
			  				var event = {
							  'summary': title,
							  'description': 'Due Date '+title+' from Taff',
							  'start': {
								'dateTime': split[0]+'T'+split[1],
								'timeZone': 'Asia/Jakarta'
							  },
							  'end': {
								'dateTime': split2[0]+'T'+split2[1],
								'timeZone': 'Asia/Jakarta'
							  },
							  'reminders': {
								'useDefault': false,
								'overrides': [
								  {'method': 'email', 'minutes': 24 * 60},
								  {'method': 'popup', 'minutes': 10}
								]
							  }
							};
							batch.add(gapi.client.calendar.events.insert({
							    'calendarId': 'primary',
							    'resource': event
							}));
							/*var request = gapi.client.calendar.events.insert({
							  'calendarId': 'primary',
							  'resource': event
							});

							request.execute(function(event) {
							  appendPre('Event created: ' + event.htmlLink);
							});*/
			  			}
			  			else if(dueDate != "" && startDate == "")
			  			{
			  				var event = {
							  'summary': title,
							  'description': 'Due Date '+title+' from Taff',
							  'start': {
								'dateTime': split2[0]+'T'+split2[1],
								'timeZone': 'Asia/Jakarta'
							  },
							  'end': {
								'dateTime': split2[0]+'T'+split2[1],
								'timeZone': 'Asia/Jakarta'
							  },
							  'reminders': {
								'useDefault': false,
								'overrides': [
								  {'method': 'email', 'minutes': 24 * 60},
								  {'method': 'popup', 'minutes': 10}
								]
							  }
							};
							batch.add(gapi.client.calendar.events.insert({
							    'calendarId': 'primary',
							    'resource': event
							}));
							/*var request = gapi.client.calendar.events.insert({
							  'calendarId': 'primary',
							  'resource': event
							});

							request.execute(function(event) {
							  appendPre('Event created: ' + event.htmlLink);
							});*/
			  			}
			  			else if(startDate != "" && dueDate == "")
			  			{
			  				var event = {
							  'summary': title,
							  'description': 'Start Date '+title+' from Taff',
							  'start': {
								'dateTime': split[0]+'T'+split[1],
								'timeZone': 'Asia/Jakarta'
							  },
							  'end': {
								'dateTime': split[0]+'T'+split[1],
								'timeZone': 'Asia/Jakarta'
							  },
							  'reminders': {
								'useDefault': false,
								'overrides': [
								  {'method': 'email', 'minutes': 24 * 60},
								  {'method': 'popup', 'minutes': 10}
								]
							  }
							};
							batch.add(gapi.client.calendar.events.insert({
							    'calendarId': 'primary',
							    'resource': event
							}));
							/*var request = gapi.client.calendar.events.insert({
							  'calendarId': 'primary',
							  'resource': event
							});

							request.execute(function(event) {
							  appendPre('Event created: ' + event.htmlLink);
							});*/
			  			}
			    });
			    batch.then(function(){
				    console.log('all jobs done!!!')
				});
				alert("Done!");
		  },
		  error: function (xhr, ajaxOptions, thrownError) {
			alert(xhr.status);
			alert(thrownError);
			alert(xhr.responseText);
		  }
		});
}

  // Client ID and API key from the Developer Console
  var CLIENT_ID = 'client-id';
  var API_KEY = 'api-key';

  // Array of API discovery doc URLs for APIs used by the quickstart
  var DISCOVERY_DOCS = ["https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest"];

  // Authorization scopes required by the API; multiple scopes can be
  // included, separated by spaces.
  var SCOPES = "https://www.googleapis.com/auth/calendar";

  var authorizeButton = document.getElementById('authorize-button');
  var signoutButton = document.getElementById('signout-button');

  /**
   *  On load, called to load the auth2 library and API client library.
   */
  function handleClientLoad() {
    gapi.load('client:auth2', initClient);
  }

  /**
   *  Initializes the API client library and sets up sign-in state
   *  listeners.
   */
  function initClient() {
    gapi.client.init({
      apiKey: API_KEY,
      clientId: CLIENT_ID,
      discoveryDocs: DISCOVERY_DOCS,
      scope: SCOPES
    }).then(function () {
    	authorizeButton.style.display = 'block';
    	authorizeButton.onclick = handleAuthClick;
    	gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
    });
  }

  /**
   *  Called when the signed in status changes, to update the UI
   *  appropriately. After a sign-in, the API is called.
   */
  function updateSigninStatus(isSignedIn) {
  	if(gapi.auth2.getAuthInstance().isSignedIn.get()==true)
		{
			//insert();
			sync();
			gapi.auth2.getAuthInstance().disconnect();
			//location.reload();
		}
  }

  /**
   *  Sign in the user upon button click.
   */
  function handleAuthClick(event) {
     gapi.auth2.getAuthInstance().signIn();
  }

  /**
   *  Sign out the user upon button click.
   */
  function handleSignoutClick(event) {

  }

  /**
   * Append a pre element to the body containing the given message
   * as its text node. Used to display the results of the API call.
   *
   * @param {string} message Text to be placed in pre element.
   */
  function appendPre(message) {
    var pre = document.getElementById('content');
    var textContent = document.createTextNode(message + '\n');
    pre.appendChild(textContent);
  }

我正在尝试向Google日历添加一个以上的事件

在localhost中,我尝试使用批处理,并且运行正常

但是当我将其移至托管服务器时,它根本无法工作。

我有一个非SSL域名,顺便说一句。

看来我的批处理根本没有被授权,我试图console.log(batch) 这给出了一个json这样的东西

  

{状态:401,状态文本:“未经授权”,标题:{…},正文:“ {↵”错误“:{↵”错误“:[↵{↵”域“:” gl…“代码”: 401,↵“ message”:“需要登录”↵}↵}↵“,结果:{…}}

问题是,我已经使用Google日历进行授权了吗?我需要对批次进行授权吗?

好的,这是我的解决方案和答案。

如果您尝试使用Quickstart示例中给出的Javascript Client在Google Calendar API上添加事件,则oAuth2授权正在使用API​​密钥。在我看来,它仅适用于本地主机。 如果您尝试添加多个事件,则需要批量处理,这是一个简单的选择,但不适用于我的服务器。 我没有使用JavaScript,而是将代码移到了PHP,并且运行良好。 Google Client PHP允许您获取访问令牌,因此您可以连续添加多个事件。

0 个答案:

没有答案