gpt4 book ai didi

c# - 检索 Outlook 建议的联系人

转载 作者:行者123 更新时间:2023-11-30 23:28:41 34 4
gpt4 key购买 nike

我的公司在 Office 365 下。我的目标是在我的 asp .net MVC 应用程序中检索用户的 Outlook 建议联系人(自动完成列表中显示的联系人)。该网站配置为使用 Windows 身份验证自动登录,我不想询问用户他的凭据。

我必须尝试使用​​ Exchange Web 服务检索建议的联系人,但我只能使用以下代码成功检索“真实”联系人:

public List<Contact> GetContacts()
{
ContactsFolder.Bind(this._service, WellKnownFolderName.Contacts);
ItemView itemView = new ItemView(1000);
itemView.PropertySet = new PropertySet(BasePropertySet.IdOnly, new PropertyDefinitionBase[4]
{
(PropertyDefinitionBase) ContactSchema.DisplayName,
(PropertyDefinitionBase) ContactSchema.Surname,
(PropertyDefinitionBase) ContactSchema.GivenName,
(PropertyDefinitionBase) ContactSchema.EmailAddress1
});
FindItemsResults<Item> items = this._service.FindItems(WellKnownFolderName.Contacts, (ViewBase) itemView);
List<Contact> list = new List<Contact>();
foreach (Item obj in items)
{
if (obj is Contact)
list.Add(obj as Contact);
}
return list;
}

然后,我尝试使用 People Api of Office 365 REST API但我不知道如何在不询问用户登录名/密码的情况下调用电话。这是一个尝试示例(如果我不使用代理,我会收到一个 HTTP 407 Error ):

    public async Task Try()
{
var proxy = WebRequest.GetSystemWebProxy();
proxy.Credentials = new NetworkCredential("foo", "1234");


// Now create a client handler which uses that proxy
HttpClient client = null;
HttpClientHandler httpClientHandler = new HttpClientHandler()
{
Proxy = proxy,
PreAuthenticate = true,
UseDefaultCredentials = false,
Credentials = new NetworkCredential("foo@foo.com", "1234")
};
var httpClient = new HttpClient(httpClientHandler);
var result = await httpClient.GetAsync("https://outlook.office.com/api/beta/me/people");

var stringContent = await result.Content.ReadAsStringAsync();
}

最佳答案

关于建议的联系人问题

我的意思是您没有在正确的文件夹中查找。根据我通过谷歌搜索所见,建议的联系人不在联系人 目录中,而是在建议的联系人 中。在您的 EWS 示例中,您正在查找联系人...参见 this discussion .另请参阅 this guy post ,他设法使用 EWS 和 powershell 访问建议的联系人文件夹,因此毫无疑问这对于 C# 和 EWS .NET SDK 是可行的。我的建议是继续尝试您的示例 1。

关于认证问题

我要强调的是,您的请求应该被授权访问 Exchange Web 服务(代码示例 1)或 outlook REST API(代码示例 2)。

在示例 1 中,我们看不到 _service 字段是如何实例化的,但我敢打赌,有一段代码或多或少看起来像下面的行,因此您可以请求 EWS。

ExchangeService service = new ExchangeService();
service.Credentials = new OAuthCredentials(token);
service.Url = new Uri(ewsUrl);

token 可能会被 Outlook REST API 重用,尝试将其设置在 httpClient

的 bearer 中

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "您的 Oauth token ");

现在您的请求应该已获得授权,但您仍然遇到代理问题。我敢打赌,这只会在您的组织内发生,因为您的 IT 设置了代理。您可能不会在生产中需要它。在本地开发时,您可以使用调试语句使其工作。

 #if DEBUG
IWebProxy webProxy = System.Net.WebRequest.DefaultWebProxy;
if (webProxy!=null && !webProxy.IsBypassed(new Uri(endpoint)))
{
client.Proxy = webProxy;
}
#endif

关于c# - 检索 Outlook 建议的联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35911745/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com