简体   繁体   中英

Python Office365 API - TimeZone stays at Local Time

I have this python code below and it works for creating a event in Outlook Calendar. The example below has Start and End time from 3pm to 4pm (I think UTC timezone)

We have users from different regions (Pacific, Mountain, Central... times). What I try to accomplish is the time always be local time. No matter where the user account from it should also default to 3pm to 4pm in their Outlook.

Thanks in advance and please let me know if I need to clarify any of this.

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

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


# Create JSON payload
data = {
  "Subject": "Testing Outlock Event",
  "Body": {
    "ContentType": "HTML",
    "Content": "Test Content"
  },
  "Start": "2016-05-23T15:00:00.000Z",
  "End": "2016-05-23T16:00:00.000Z",
      "Attendees": [
    {
      "EmailAddress": {
        "Address": "user1@domain.com",
        "Name": "User1"
      },
       "Type": "Required"  },

       {
      "EmailAddress": {
        "Address": "user2@domain.com",
        "Name": "User2"
      },
       "Type": "Optional"  }
  ]
}

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)

First of all, there's no point of creating a meeting on the same time at different timezones.

If you really want to do so, you'll need to create separate meeting requests per the timezone of the attendees, the JSON format would be something like below,

var body = new JObject
        {
            {"Subject", "Testing Outlock Event"},
            {"Start", new JObject { { "DateTime", "2016-03-24T15:00:00"}, { "TimeZone", "Pacific Standard Time" } } },
            {"End", new JObject { { "DateTime", "2016-03-24T16:00:00"}, { "TimeZone", "Pacific Standard Time" } } }
        };

Note you need to remove the capital 'Z' from the time, so that it'll be shown in the local time tone specified by the "TimeZone" attribute.

The workaround above needs to know the timezone of all the attendees, I'm not sure if this can be done pragmatically. If not, it might not make much sense if you have tones of attendees.

The "ideal" way of achieving your requirement is giving the time without specifying the timezone, so that it'll be shown at the same time with different timezone.

However, it's not supported yet, but you can vote here https://officespdev.uservoice.com/forums/224641-general/suggestions/12866364-microsoft-graph-o365-unified-api-create-events-w

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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