python - Microsoft Outlook 图形事件 API : how to get different timezone?

标签 python microsoft-graph-api outlook-restapi

我正在尝试获取 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)

更新:

如果有人来这里询问此类问题,您需要更新 header 以发送任何特定时区。以下是更新 header ,请确保将时区用双引号引起来:

  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"

关于python - Microsoft Outlook 图形事件 API : how to get different timezone?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50240296/

相关文章:

python datapanda : getting values from rows into list

asp.net - 获取成员组 “Insufficient privileges to complete the operation”

azure - 使用POSTMAN获取授权码-OAuth2.0

office365 - Microsoft Graph - 修补日历事件时出现 PopAuthenticatorInvalidSignature 错误

json - 图形订阅已创建但未发送通知

api - Office 365 REST API列表资源( session 室)

python - 修改 Dijkstra 算法以获得最少的变化

python - 让 Fabric Python 库工作

javascript - 无法让高速公路示例工作(js 和 python)

reactjs - 如何向Azure应用程序授予Calendar.ReadWrite.Shared权限?