Dave Clark
Dave Clark

Reputation: 2303

Authenticate and query the Dynamics CRM Web API using Angular 2 (TypeScript)

How do you authenticate with and query the Dynamics CRM Web API from within a SPA built using Angular 2 (TypeScript)?

Research so far has suggested that:

My attempts so far have involved cloning various Angular 2 repos built with ADAL, such as this one, and editing their configuration files to point towards my Azure application. These attempts have all resulted in 401 (Unauthorized) cross-domain errors which is detailed in my separate question.

Using the same configuration (e.g. cliendId) in this JavaScript walk-through from Microsoft is successful.

Upvotes: 1

Views: 1703

Answers (2)

Dave Clark
Dave Clark

Reputation: 2303

The code sample provided by Fei Xue posted here on GitHub has proved successful in authenticating with a Dynamics CRM application hosted in Azure and querying the Web API.

Similar code samples I've used have resulted in 401 Unauthorized HTTP errors. Why this sample works and others do not remains unanswered for the moment.

Upvotes: 1

Fei Xue
Fei Xue

Reputation: 14649

This Access-Control-Allow-Origin issue is more relative to the server-side. It doesn't matter you were developing with Angular 1 or Angular 2.

You can confirm this issue send the options request manually to verity whether your domain is allowed to request. For example, we can check whether the AJAX request from http://localhost:3000 is allowed using the request below:

OPTIONS: https://xxxx.crm.dynamics.com/api/data/v8.0/accounts
Authorization: Bearer {accessToken}
Origin: http://localhost:3000
Host: xxxx.crm.dynamics.com
Access-Control-Request-Headers: authorization
Access-Control-Request-Method: GET

And if it will return the response like below if the domain is allowed( refer Access-Control-Allow-Origin): enter image description here

To fix this issue, you need to check whether the server supports the CORS. If it doesn't allowed, you can create a web API as the proxy for a workaround.

Upvotes: 1

Related Questions