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