使用Microsoft Graph API v.1.0分页消息

时间:2018-01-05 17:02:52

标签: microsoft-graph

我正在使用Microsoft Graph API v.1.0我试图获取电子邮件我只看到其中10个我理解的是默认的分页页面限制但是没有@odata:nextLink属性被返回去下一批结果。

这是我的代码:

$graph = new Graph();
$graph->setAccessToken(session('access_token'));

$me = $graph->createRequest("get", "/me/messages")
            ->setReturnType(Model\User::class)
            ->execute();
return $me;

结果:

   array:10 [▼
  0 => User {#379 ▼
    #_propDict: array:28 [▶]
  }
  1 => User {#365 ▶}
  2 => User {#375 ▶}
  3 => User {#368 ▶}
  4 => User {#367 ▶}
  5 => User {#366 ▶}
  6 => User {#380 ▶}
  7 => User {#373 ▶}
  8 => User {#382 ▶}
  9 => User {#383 ▼
    #_propDict: array:28 [▼
      "@odata.etag" => "W/"CQAAABYAAAC8b+tAO4nLRZCbkhud5CXFAANG4sRR""
      "id" => "AAMkADdlZTBjNjQ4LWI0OGItNDFhZS05ZDNiLThiY2JkYzIzZWZkYwBGAAAAAABFX7lJCx7ZRLTJ6iI0yZK6BwC8b_tAO4nLRZCbkhud5CXFAAAAAAEKAAC8b_tAO4nLRZCbkhud5CXFAANGMK1oAAA="
      "createdDateTime" => "2018-01-05T14:25:58Z"
      "lastModifiedDateTime" => "2018-01-05T14:25:58Z"
      "changeKey" => "CQAAABYAAAC8b+tAO4nLRZCbkhud5CXFAANG4sRR"
      "categories" => []
      "receivedDateTime" => "2018-01-05T14:25:58Z"
      "sentDateTime" => "2018-01-05T14:25:58Z"
      "hasAttachments" => false
      "internetMessageId" => "<DB5PR05MB15093E653073F8E1A95F0EE2D11C0@DB5PR05MB1509.eurprd05.prod.outlook.com>"
      "subject" => "test"
      "bodyPreview" => "HTML"
      "importance" => "normal"
      "parentFolderId" => "AQMkADdlZQAwYzY0OC1iNDhiLTQxYWUtOWQzYi04YmNiZGMyM2VmZGMALgAAA0VfuUkLHtlEtMnqIjTJkroBALxv60A7ictFkJuSG53kJcUAAAIBCgAAAA=="
      "conversationId" => "AAQkADdlZTBjNjQ4LWI0OGItNDFhZS05ZDNiLThiY2JkYzIzZWZkYwAQAN7fN0jAEi5LroDvBtBQMRs="
      "isDeliveryReceiptRequested" => false
      "isReadReceiptRequested" => false
      "isRead" => true
      "isDraft" => false
      "webLink" => "https://outlook.office365.com/owa/?ItemID=AAMkADdlZTBjNjQ4LWI0OGItNDFhZS05ZDNiLThiY2JkYzIzZWZkYwBGAAAAAABFX7lJCx7ZRLTJ6iI0yZK6BwC8b%2BtAO4nLRZCbkhud5CXFAAAAAAEK ▶"
      "inferenceClassification" => "focused"
      "body" => array:2 [▶]
      "sender" => array:1 [▶]
      "from" => array:1 [▶]
      "toRecipients" => array:1 [▶]
      "ccRecipients" => []
      "bccRecipients" => []
      "replyTo" => []
    ]
  }
]

2 个答案:

答案 0 :(得分:0)

请参阅PHP文档,了解https://github.com/microsoftgraph/msgraph-sdk-php/wiki/Example-calls#get-a-collection-of-items

分页

看起来您应该更改返回类型以匹配端点。在您的情况下,您的返回类型为User,但您要查询邮件。这就是结果数组有User个对象的原因。我认为您正在寻找的模型类型是消息,您可以在https://github.com/microsoftgraph/msgraph-sdk-php/blob/dev/src/Model/Message.php

查看此消息来源

答案 1 :(得分:0)

我最终直接使用API​​并格式化下一个和上一个链接。

from typing import List, Dict


class CollegeClass:
    def __init__(self, class_code: str, name: str):
        self.class_code = class_code  #type: str
        self.name = name

    def __str__(self):
        return f"{self.class_code}, {self.name}"

    def __repr__(self):
        return str(self)


class Student:
    def __init__(self):
        self.current_classes = dict()   # type: Dict[str, CollegeClass]

    def enroll(self, college_class: CollegeClass):
        if college_class.class_code in self.current_classes:
            print("The student is already taking the class.")
            return
        self.current_classes[college_class.class_code] = college_class


if __name__ == '__main__':

    math = CollegeClass("mth101", "Intro to Math")
    english = CollegeClass("eng201", "Intro to Fiction")

    eric_praline = Student()
    eric_praline.enroll(math)
    eric_praline.enroll(english)

    for _, cls in eric_praline.current_classes.items():
        print(cls)