Microsoft outlook 图形事件 api:如何获得不同的时区?

Microsoft outlook graph events api: how to get different timezone?

我正在尝试获取 Microsoft 日历活动。我的 outlook 帐户的默认时区是美国东部时区。但是我从 rest api 调用中得到的响应都是 UTC 格式的。如何获得我的默认时区,即美国东部时间?

这是我的代码:

def make_api_call(method, url, token, payload = None, parameters = None):
  headers = { 'User-Agent' : 'python_tutorial/1.0',
              'Authorization' : 'Bearer {0}'.format(token),
              'Accept' : 'application/json'}

  request_id = str(uuid.uuid4())
  instrumentation = { 'client-request-id' : request_id,
                      'return-client-request-id' : 'true' }

  headers.update(instrumentation)

  response = None

  if (method.upper() == 'GET'):
      response = requests.get(url, headers = headers, params = parameters)
  elif (method.upper() == 'POST'):
      headers.update({ 'Content-Type' : 'application/json' })
      response = requests.post(url, headers = headers, data = json.dumps(payload), params = parameters)

  return response

def get_my_events(access_token, start_date_time, end_date_time):
    get_events_url = graph_endpoint.format('/me/calendarView')
    query_parameters = {'$top': '10',
    '$select': 'subject,start,end,location',
    '$orderby': 'start/dateTime ASC',
    'startDateTime': start_date_time,
    'endDateTime': end_date_time}

    r = make_api_call('GET', get_events_url, access_token, parameters = query_parameters)
    if (r.status_code == requests.codes.ok):
        return r.json()
    else:
        return "{0}: {1}".format(r.status_code, r.text)

更新:

还有其他人来这里问这类问题,您需要更新 headers 以发送任何特定时区。这是更新 headers,确保用双引号将时区括起来:

  headers = { 'User-Agent' : 'python_tutorial/1.0',
              'Authorization' : 'Bearer {0}'.format(token),
              'Accept' : 'application/json',
              'Prefer': 'outlook.timezone="Eastern Standard Time"'}

您需要使用 Prefer: outlook.timezone header 指定时区。

来自documentation

Prefer: outlook.timezone

Use this to specify the time zone for start and end times in the response. If not specified, those time values are returned in UTC.

例如,要将其设置为美国东部,您可以发送

Prefer: outlook.timezone="Eastern Standard Time"