如何解决EWS GetUserPhoto 从 Basic Auth 切换到 OAuth
我一直在使用基本身份验证来获取用户照片,如下所示。
string email = "SomeEmail@email.com";
HttpWebRequest request = WebRequest.Create(string.Format("https://outlook.office365.com/EWS/Exchange.asmx/s/GetUserPhoto?email={0}&size=HR648x648",email)) as HttpWebRequest;
request.Credentials = new NetworkCredential("SomeID","SomePwd");
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
Stream stream = response.GetResponseStream();
using (MemoryStream ms = new MemoryStream())
{
string encodedPhoto = Convert.ToBase64String((ms.ToArray()));
}
}
但由于 EWS 的基本身份验证将被停用,我正在尝试将 OAuth 2.0 用于同一请求。这是我迄今为止尝试过的。
var pcaOptions = new PublicClientApplicationOptions
{
ClientId = ConfigurationManager.AppSettings["appId"],TenantId = ConfigurationManager.AppSettings["tenantId"]
};
var pca = PublicClientApplicationBuilder.CreateWithApplicationOptions(pcaOptions).Build();
var ewsScopes = new string[] { "https://outlook.office365.com/EWS.AccessAsUser.All" };
var authResult = await pca.AcquireTokenInteractive(ewsScopes).ExecuteAsync();
var ewsClient = new ExchangeService();
string email = "SomeEmail@Email.com";
ewsClient.Url = new Uri(string.Format("https://outlook.office365.com/EWS/Exchange.asmx/s/GetUserPhoto?email={0}&size=HR648x648",email));
ewsClient.Credentials = new OAuthCredentials(authResult.AccessToken);
我如何才能从这里获取用户照片?任何帮助或信息将不胜感激。
解决方法
我建议您使用 Microsoft Graph API 来获取用户照片。参考,https://docs.microsoft.com/en-us/graph/api/profilephoto-get?view=graph-rest-1.0
使用 Graph Explorer 试试
Graph .Net SDK 入门 https://docs.microsoft.com/en-us/graph/sdks/sdks-overview
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var stream = await graphClient.Me.Photo.Content
.Request()
.GetAsync();
,
您不需要使用 EWS 托管 API,您只需修改现有代码以包含访问令牌,例如
ar
或者如果您确实想使用 EWS 托管 API,您可以使用类似
string email = "SomeEmail@email.com";
HttpWebRequest request = WebRequest.Create(string.Format("https://outlook.office365.com/EWS/Exchange.asmx/s/GetUserPhoto?email={0}&size=HR648x648",email)) as HttpWebRequest;
request.Headers.Add("Authorization","Bearer " + authResult.AccessToken);
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
Stream stream = response.GetResponseStream();
using (MemoryStream ms = new MemoryStream())
{
string encodedPhoto = Convert.ToBase64String((ms.ToArray()));
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。