简体   繁体   English

Python 微软图 api

[英]Python microsoft graph api

I am using microsoft graph api to pull my emails in python and return them as a json object.我正在使用 microsoft graph api 将我的电子邮件拉到 python 并将它们作为 json ZA8CFDE6331BD49EB62AC96F8911C66 There is a limitation that it only returns 12 emails.有一个限制,它只返回 12 封电子邮件。 The code is:代码是:

def get_calendar_events(token):
  graph_client = OAuth2Session(token=token)

  # Configure query parameters to
  # modify the results
  query_params = {
    #'$select': 'subject,organizer,start,end,location',
    #'$orderby': 'createdDateTime DESC'
    '$select': 'sender, subject', 
    '$skip': 0,
    '$count': 'true'
  }

  # Send GET to /me/events
  events = graph_client.get('{0}/me/messages'.format(graph_url), params=query_params)
  events = events.json()
  # Return the JSON result
  return events

The response I get are twelve emails with subject and sender, and total count of my email.我收到的回复是十二封带有主题和发件人的电子邮件,以及我的 email 的总数。 Now I want iterate over emails changing the skip in query_params to get the next 12. Any method of how to iterate it using loops or recursion.现在我想遍历电子邮件,更改 query_params 中的跳过以获得下一个 12。如何使用循环或递归对其进行迭代的任何方法。

I'm thinking something along the lines of this:我正在考虑这样的事情:

def get_calendar_events(token):
  graph_client = OAuth2Session(token=token)

  # Configure query parameters to
  # modify the results
  json_list = []
  ct = 0
  while True:
      query_params = {
        #'$select': 'subject,organizer,start,end,location',
        #'$orderby': 'createdDateTime DESC'
        '$select': 'sender, subject', 
        '$skip': ct,
        '$count': 'true'
      }

      # Send GET to /me/events
      events = graph_client.get('{0}/me/messages'.format(graph_url), params=query_params)
      events = events.json()
      json_list.append(events)
      ct += 12
  # Return the JSON result
  return json_list

May require some tweaking but essentially you're adding 12 to the offset each time as long as it doesn't return an error.可能需要进行一些调整,但本质上,只要不返回错误,您每次都会在偏移量中添加 12。 Then it appends the json to a list and returns that.然后它将 json 附加到列表并返回。

If you know how many emails you have, you could also batch it that way.如果你知道你有多少封电子邮件,你也可以这样批处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM