【发布时间】: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