簡體   English   中英

在Office365 Rest API中創建多個事件

[英]Creating multiple events in office365 rest api

我下面的代碼將一個日歷事件發布到Office 365 REST API上的事件。 我需要在日歷中輸入約100個事件。 有什么辦法可以在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)

批處理已在我們的路線圖上,但今天還沒有。

我最終使函數一次創建一個事件,並在每次迭代時調用該函數:

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