I am new to Microsoft Graph and I want to use it to read and move email messages. This is my C# code:
static async Task Main(string[] args)
{
var scopes = new[] { "https://graph.microsoft.com/.default" }
var tenantId = args[0];
var clientId = args[1];
var clientSecret = args[2];
var options = new TokenCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
}
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret, options)
var graphClient = new GraphServiceClient(clientSecretCredential, scopes)
var messsages = await graphClient.Me.Messages.GetAsync();
Console.WriteLine(messsages.Value.Count);
Console.WriteLine("Hello, World!");
}
This is the error message I got:
C:\Users\daan\source\nonrepos\TryMicrosoftGraph\TryOther\Program.cs(26,31): warning CS8602: Dereference of a possibly null reference. [C:\Users\daan\source\nonrepos\TryMicrosoftGraph\TryOther\TryOther.csproj] C:\Users\daan\source\nonrepos\TryMicrosoftGraph\TryOther\Program.cs(26,31): warning CS8602: Dereference of a possibly null reference. [C:\Users\daan\source\nonrepos\TryMicrosoftGraph\TryOther\TryOther.csproj] Unhandled exception. Microsoft.Graph.Models.ODataErrors.ODataError: /me request is only valid with delegated authentication flow.
so the message is:
/me request is only valid with delegated authentication flow.
I am not sure what to do now. Do I really need a "delegated authentication flow". Or is there an easier way to access my messages?
I've seen such errors before on stackoverflow but such posts do not explain how I resolve it in a way it gives me access to the emails.


Your application is a confidential application that requests a token through the client credential flow. This is why the /me path is not functioning as mentioned in the error notification.
Essentially, you still have access to messages from any user within your organization/tenant, but you need to construct a different URL to retrieve those messages.
For instance, you could use this URL to retrieve messages for a user:
Another interesting requests that might help you: