EWS GetUserPhoto 从基本身份验证切换到 OAuth
EWS GetUserPhoto switch from Basic Auth to OAuth
我一直在使用 Basic Auth 获取用户照片,如下所示。
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 Managed 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()));
}
}
或者如果您确实想使用 EWS 托管 API,您可以使用
String ETag = "";
GetUserPhotoResults grPhoto = service.GetUserPhoto("user@domain.com", "HR240x240", ETag);
if (grPhoto.Status == GetUserPhotoStatus.PhotoReturned)
{
ETag = grPhoto.EntityTag;
}
grPhoto = service.GetUserPhoto("user@domain.com", "HR240x240", ETag);
switch (grPhoto.Status)
{
case GetUserPhotoStatus.PhotoReturned: ETag = grPhoto.EntityTag;
break;
case GetUserPhotoStatus.PhotoUnchanged:
Console.WriteLine("Photo Unchanged");
break;
}
我一直在使用 Basic Auth 获取用户照片,如下所示。
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 Managed 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()));
}
}
或者如果您确实想使用 EWS 托管 API,您可以使用
String ETag = "";
GetUserPhotoResults grPhoto = service.GetUserPhoto("user@domain.com", "HR240x240", ETag);
if (grPhoto.Status == GetUserPhotoStatus.PhotoReturned)
{
ETag = grPhoto.EntityTag;
}
grPhoto = service.GetUserPhoto("user@domain.com", "HR240x240", ETag);
switch (grPhoto.Status)
{
case GetUserPhotoStatus.PhotoReturned: ETag = grPhoto.EntityTag;
break;
case GetUserPhotoStatus.PhotoUnchanged:
Console.WriteLine("Photo Unchanged");
break;
}