我在获取 OAuth 凭据以在 Office 365 中使用 EWS 时遇到问题。在较高级别上,我正在编写一个 SharePoint 2013 应用程序,并且我正在尝试访问用户在 Exchange 中的邮箱数据。我确实通过将 OAuth 代码换成硬编码的用户名和密码来验证我的 EWS 代码是否“正确”,并且效果很好。
我使用下面的代码获得了一个令牌,但是当我尝试访问用户的收件箱时,我得到了 401。为简洁起见,我将其关闭,但在访问收件箱之前,我将令牌传递到新的 OAuthCredentials 对象中。
string acsUrl = "https://accounts.accesscontrol.windows.net/";
using (WebClient exchangeTokenClient = new WebClient())
{
exchangeTokenClient.BaseAddress = acsUrl;
NameValueCollection requestParams = new NameValueCollection();
requestParams.Add("grant_type", "client_credentials");
requestParams.Add("client_id", "<clientid>@<realm>");
requestParams.Add("client_secret", "<client secret>");
requestParams.Add("resource", "00000002-0000-0ff1-ce00-000000000000/outlook.office365.com@<realm>");
exchangeTokenClient.Headers.Add("Authorization", "Bearer " + ((SharePointAcsContext)spContext).UserAccessTokenForSPAppWeb);
byte[] responseBytes = exchangeTokenClient.UploadValues("<realm>/tokens/OAuth/2", "POST", requestParams);
string response = Encoding.UTF8.GetString(responseBytes);
}
我想得越多,我就越想知道我的“应用程序”是否需要交换服务器上的权限,以及这是否是 401 的根本原因。
有没有人真的这样做过?我觉得这应该是可能的,但我似乎找不到很多关于这个过程的文档。
谢谢
乔