简体   繁体   English

在Office365 Rest API中创建多个事件

[英]Creating multiple events in office365 rest api

I have below code which posts one calendar event to the events on office 365 REST API. 我下面的代码将一个日历事件发布到Office 365 REST API上的事件。 I need to enter about 100 events to my calendar. 我需要在日历中输入约100个事件。 Is there any way to place multiple events in the json data or should I use for loop? 有什么办法可以在json数据中放置多个事件,还是应该使用for循环?

import urllib2
import getpass
import os
import json
import sys
import base64


# Set the request parameters
url = 'https://outlook.office365.com/api/v1.0/me/events?$Select=Start,End'
user = 'emailuser@email.com'

pwd = getpass.getpass('Please enter your AD password: ')


# Create JSON payload
data = {
  "Subject": "My Subject",
  "Body": {
    "ContentType": "HTML",
    "Content": ""
  },
  "Start": "2015-08-11T07:00:00-05:00",
  "StartTimeZone": "Central Standard Time",
  "End": "2015-08-11T15:00:00-05:00",
  "EndTimeZone": "Central Standard Time",
}

json_payload = json.dumps(data)

# Build the HTTP request
opener = urllib2.build_opener(urllib2.HTTPHandler)
request = urllib2.Request(url, data=json_payload)
auth = base64.encodestring('%s:%s' % (user, pwd)).replace('\n', '')
request.add_header('Authorization', 'Basic %s' % auth)
request.add_header('Content-Type', 'application/json')
request.add_header('Accept', 'application/json')
request.get_method = lambda: 'POST'
# Perform the request
result = opener.open(request)

批处理已在我们的路线图上,但今天还没有。

I ended up making function to create one event at a time and call the function on each iteration: 我最终使函数一次创建一个事件,并在每次迭代时调用该函数:

def create_event(date1, date2):
    # Create JSON payload
    data = {
      "Subject": admin.longname,
      "Body": {
        "ContentType": "HTML",
        "Content": ""
      },
      "Start": date1,
      "StartTimeZone": "Central Standard Time",
      "End": date2,
      "EndTimeZone": "Central Standard Time",
    }

    json_payload = json.dumps(data)

    # Build the HTTP request
    opener = urllib2.build_opener(urllib2.HTTPHandler)
    request = urllib2.Request(url, data=json_payload)
    auth = base64.encodestring('%s:%s' % (user, pwd)).replace('\n', '')
    request.add_header('Authorization', 'Basic %s' % auth)
    request.add_header('Content-Type', 'application/json')
    request.add_header('Accept', 'application/json')
    request.get_method = lambda: 'POST'
    # Perform the request
    result = opener.open(request)

def A( weeka, weekb ):
    today = datetime.date.today()
    firstday = today + relativedelta(weekday=SU(+ weeka))
    for i in range(5):
        firstday += datetime.timedelta(days=1)
        date1 = '%sT07:00:00-05:00' % firstday
        date2 = '%sT16:00:00-05:00' % firstday
        create_event(date1, date2)

A(1,2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM