【问题标题】:Microsoft outlook graph events api: how to get different timezone?Microsoft Outlook 图形事件 api:如何获取不同的时区?
【发布时间】:2018-05-08 18:40:22
【问题描述】:

我正在尝试获取 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 = { 'User-Agent' : 'python_tutorial/1.0',
              'Authorization' : 'Bearer {0}'.format(token),
              'Accept' : 'application/json',
              'Prefer': 'outlook.timezone="Eastern Standard Time"'}

【问题讨论】:

  • 我不熟悉 Outlook api,但是是什么阻止您使用 datetime.timedelta 自己进行转换?

标签: python microsoft-graph-api outlook-restapi


【解决方案1】:

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

来自documentation

首选:outlook.timezone

使用它来指定响应中开始和结束时间的时区。如果未指定,则以 UTC 格式返回这些时间值。

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

Prefer: outlook.timezone="Eastern Standard Time"

【讨论】:

  • 谢谢马克。 Headers 是一个字典,知道如何在 headers 字典中指定它吗?
  • 我知道了,Marc 并为其他来这里询问此类问题的人编辑了我的问题。感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-30
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 2012-08-06
  • 1970-01-01
相关资源
最近更新 更多