0

我正在尝试找出一种从 Outlook Office 365 api 中的子文件夹返回消息的方法。一切似乎都指向这一点;

HttpResponseMessage response = await client.GetAsync("https://outlook.office365.com/api/v1.0/me/folders/inbox/childfolders/Odata/messages");

但我总是收到一个错误的请求。

这是我的资源。

MSDN

谢谢斯科特

4

2 回答 2

0

URL 语法为:

https://outlook.office365.com/api/v1.0/me/folders/<FOLDER ID>/Messages

因此,您需要获取要查询的文件夹的 ID。例如,如果它是收件箱的子文件夹,您可以执行 GET 来:

https://outlook.office365.com/api/v1.0/me/folders/inbox/childfolders

你会得到类似的东西:

{
  "@odata.context": "https://outlook.office365.com/api/v1.0/$metadata#Me/Folders('inbox')/ChildFolders",
  "value": [
    {
      "@odata.id": "https://outlook.office365.com/api/v1.0/Users('JasonJ@contoso.com')/Folders('AAMkADNhMjcxM2U5LWY2MmItNDRjYy05YzgwLWQwY2FmMTU1MjViOAAuAAAAAAC_IsPnAGUWR4fYhDeYtiNFAQCDgDrpyW-uTL4a3VuSIF6OAAAeY0W3AAA=')",
      "Id": "AAMkADNhMjcxM2U5LWY2MmItNDRjYy05YzgwLWQwY2FmMTU1MjViOAAuAAAAAAC_IsPnAGUWR4fYhDeYtiNFAQCDgDrpyW-uTL4a3VuSIF6OAAAeY0W3AAA=",
      "ParentFolderId": "AAMkADNhMjcxM2U5LWY2MmItNDRjYy05YzgwLWQwY2FmMTU1MjViOAAuAAAAAAC_IsPnAGUWR4fYhDeYtiNFAQCDgDrpyW-uTL4a3VuSIF6OAAAAAAEMAAA=",
      "DisplayName": "New Subfolder",
      "ChildFolderCount": 0
    }
  ]
}

然后获取该Id字段的值并将其插入 URL:

https://outlook.office365.com/api/v1.0/me/folders/AAMkADNhMjcxM2U5LWY2MmItNDRjYy05YzgwLWQwY2FmMTU1MjViOAAuAAAAAAC_IsPnAGUWR4fYhDeYtiNFAQCDgDrpyW-uTL4a3VuSIF6OAAAeY0W3AAA=/Messages
于 2015-04-21T13:08:38.987 回答
0
public void EnsureConnectionValid()
    {
        if (AuthenticationContext == null)
        {
            AuthenticationContext = new AuthenticationContext(authority);
            AuthenticationResult = AuthenticationContext.AcquireToken(resource, clientId, new Uri(redirectUri), PromptBehavior.Auto);
        }
    }

    public async Task<string> GetFolderId(string Path)
    {
        EnsureConnectionValid();
        var client = new HttpClient();
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AuthenticationResult.AccessToken);
        var restCommand = "https://outlook.office365.com/api/v1.0/me/folders/Inbox/childfolders?$filter=DisplayName eq " + "'" + Path + "'";
        HttpResponseMessage response = await client.GetAsync(restCommand);
        response.EnsureSuccessStatusCode();
        string jsonMessage;
        using (var responseStream = await response.Content.ReadAsStreamAsync())
        {
            jsonMessage = new StreamReader(responseStream).ReadToEnd();
        }
        var folderObject = JObject.Parse(jsonMessage)["value"].ToObject<FoldersList[]>();
        return folderObject.Select(r => r.Id).SingleOrDefault();
    }
于 2015-04-20T15:33:59.283 回答