Frank
Frank

Reputation: 5

Office 365 Rest Api Having issues getting access token

So far i have this.

        public static async Task<OutlookServicesClient> CreateOutlookClientAsync(string capability)
        {
            try
            {

                string authority = CommonAuthority;

                // Create an AuthenticationContext using this authority.
                _authenticationContext = new AuthenticationContext(authority);

                //See the Discovery Service Sample (https://github.com/OfficeDev/Office365-Discovery-Service-Sample)
                //for an approach that improves performance by storing the discovery service information in a cache.
                DiscoveryClient discoveryClient = new DiscoveryClient(
                    async () => await GetTokenHelperAsync(_authenticationContext, DiscoveryResourceId));

                // Get the specified capability ("Contacts").
                CapabilityDiscoveryResult result =
                    await discoveryClient.DiscoverCapabilityAsync(capability);
                var client = new OutlookServicesClient(
                    result.ServiceEndpointUri,
                    async () =>
                        await GetTokenHelperAsync(_authenticationContext, result.ServiceResourceId));
                return client;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                if (_authenticationContext != null && _authenticationContext.TokenCache != null)
                    _authenticationContext.TokenCache.Clear();
                return null;
            }
        }

    }

 private static async Task<string> GetTokenHelperAsync(AuthenticationContext context, string resourceId)
        {
            string accessToken = null;
            AuthenticationResult result = null;
            string myId = WebConfigurationManager.AppSettings["ida:ClientID"];
            string myKey = WebConfigurationManager.AppSettings["ida:Password"];
            ClientCredential client = new ClientCredential(myId,myKey);
            
            result = await context.AcquireTokenAsync(resourceId, client);
            //result =context.AcquireToken(resourceId, ClientID,_returnUri);
            accessToken = result.AccessToken;


            return accessToken;
        }

When i get to result one of two things happen if i user AcquireTokenAsync i get an error stating Application with identifier XXXX was not found in directory api.office.com otherwise if i run AcquireToken i get the login modal to pop but an error occurs indicating the request must contain client_secret .

I have no idea how to resolve this issue i suspect it may have something to do with the actual app configuration i have tried both creating my own app in Azure AD and using VS Connected Service, Has Anyone Else ran into a similar issues?

Upvotes: 0

Views: 1801

Answers (1)

Jason Johnston
Jason Johnston

Reputation: 17702

Based on the errors you're seeing, there seems to be an issue with how your app is registered. The first error usually happens when the app is not marked as multi-tenant, and you login to the app with a tenant other than the one where the app is registered.

The second error is odd. Client secret is what you're reading out of the ida:Password element and passing in the ClientCredential object.

I just put a .NET tutorial up yesterday that walks through setting this stuff up. Take a look and see if that helps get you unblocked.

Upvotes: 1

Related Questions