I am trying to read an email attachment using Microsoft Graph API(v2) but I am getting "Object reference not set to an instance" error
I have verified the app permissions in Azure Active Directory and has all the permissions set mentioned in microsoft documentation
Mail.Read Group.Read.All (Added extra permission to try) I am able to read the message and its content correctly and even the message "HasAttachments" property is true but when I call API again to fetch attachments it gives error.
Also tried to create new token for fetching attachments considering the fact that existing one would have expired but no luck
OutlookServicesClient client = new OutlookServicesClient(new Uri(OutlookAPI), GetAccessToken);
client.Context.SendingRequest2 +=
new EventHandler<Microsoft.OData.Client.SendingRequest2EventArgs>((sender, e) => InsertXAnchorMailboxHeader(sender, e, userEmail));
var attachments = await client.Me.MailFolders.GetById(constInbox).Messages.GetById(strMessageID).Attachments.ExecuteAsync();
public async Task<string> GetAccessToken()
{
string accessToken = null;
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(TokenUri);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var requestContent = string.Format("grant_type=password" + "&client_id=" + appId +
"&client_secret=" + appPassword +
"&resource=" + Resource +
"&username=" + userEmail +
"&password=" + passwd);
var content = new StringContent(requestContent, Encoding.UTF8, "application/x-www-form-urlencoded");
var response = client.PostAsync(client.BaseAddress, content).Result;
var result = response.Content.ReadAsStringAsync().Result;
var jobject = JsonConvert.DeserializeObject<JObject>(result);
accessToken = jobject.GetValue("access_token").ToString();
}
return accessToken;
}