I have been using Basic Auth to get user photo as below.
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()));
}
}
But since Basic Authentication for EWS will be decommissioned, I'm trying to use OAuth 2.0 for the same request. This is what I've tried so far.
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);
How can I proceed to get user photo from here? Any help or information will be very much appreciated.
You don't need to use the EWS Managed API you can just modify you existing code to include the Access token eg
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()));
}
}
or if you do want to use the EWS managed API you can use something like
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;
}