3
votes

I want to get access token to use it to fetch email from outlook using microsoft graph api. My application will be Console based c# application which will run automatically after every 20 min and will fetch the email.

I am new to c# as well as microsoft graph, this is my first task related to these technology.

Problem is:

When i tried to fetch token using client_Credentials i was successfully able to do so, but now that token is expired i want to get new token and if I try to generate new token it is returning the expired one only.

Relevant code:

result = await context.AcquireTokenAsync(resourceUri, clientCredential);

Using AcquireTokenSilentAsync method return as error: "Failed to acquire token silently as no token was found in the cache. Call method AcquireToken."

Relevant code:

result = await authContext.AcquireTokenSilentAsync(resourceUri, clientId);

My questions:

  1. Is accessing token using client credential is correct way to fulfill my need?

  2. I have read that using client_Credentials we do not need refresh_token, every time we try to connect we will get new token.

  3. How to get new token every time I want to connect?

  4. Any extra suggestion about how to approach to my main objective which are not asked in question would be dearly welcomed.

I'm attaching my code sample:

static async Task getAccessToken()
{
    authContext = new AuthenticationContext("https://login.microsoftonline.com/<tenantId>");
    try
    {
        result = await authContext.AcquireTokenSilentAsync(resourceUri, clientId);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
        try
        {

            result = await authContext.AcquireTokenAsync(resourceUri, clientCredential);

            Console.WriteLine("" + result.AccessToken+"\n\n");
        }
        catch (Exception e)
        {
            Console.WriteLine("\n AcquireTokenAsync failed\n");
            Console.WriteLine(""+e);
        }
    }
    if (result == null)
    {
        Console.WriteLine("Canceling attempt to get access token.\n");
        return;
    }
    Console.WriteLine(result.AccessToken);

}
1

1 Answers

2
votes

You're mixing a two different OAuth flows (Authorization Code and Client Credentials). You should only need to call AcquireTokenAsync with the correct credentials. Whenever you need a new token (each token lives about an hour), you re-execute this method to get a new token:

static async Task<AuthenticationResult> getAccessToken()
{
    ClientCredential clientCredential = new ClientCredential("YOUR_APP_ID", "YOUR_APP_SECRET");
    AuthenticationContext authContext = new AuthenticationContext("https://login.microsoftonline.com/YOUR_TENANT_ID");
    AuthenticationResult result = null;

    try
    {
        result = await authContext.AcquireTokenAsync("https://graph.microsoft.com", clientCredential);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }

    if (result == null)
        Console.WriteLine("Canceling attempt to get access token.");
    else
        Console.WriteLine(result.AccessToken);

    return result;
}