/// <summary> /// /// </summary> /// <param name="apiUrl">CRM API base URL e.g. https://orgname.api.crm.dynamics.com/api/data/v8.0/ </param> /// <param name="accessToken">allows for hard coded access token for testing</param> /// <param name="callerID">user id to impersonate on calls</param> /// <param name="getAccessToken">method to call to refresh access token, called before each use of token</param> public CRMWebAPI(string apiUrl, string accessToken, Guid callerID = default(Guid), Func <string, Task <string> > getAccessToken = null) { _crmWebAPIConfig = new CRMWebAPIConfig { APIUrl = apiUrl, AccessToken = accessToken, CallerID = callerID, GetAccessToken = getAccessToken }; _httpClient = new HttpClient(); _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _crmWebAPIConfig.AccessToken); SetHttpClientDefaults(callerID); }
/// <summary> /// Instantiate the CRMWebAPI using the CRMWebAPIConfig, if NetworkCredentials are present it is assumed a on-premisse connection type. /// </summary> /// <param name="crmWebAPIConfig"> Api Config Object, it contais the </param> public CRMWebAPI(CRMWebAPIConfig crmWebAPIConfig) { _crmWebAPIConfig = crmWebAPIConfig; if (_crmWebAPIConfig.NetworkCredential != null) { _httpClient = new HttpClient(new HttpClientHandler { Credentials = _crmWebAPIConfig.NetworkCredential }); } else { _httpClient = new HttpClient(); _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _crmWebAPIConfig.AccessToken); } SetHttpClientDefaults(_crmWebAPIConfig.CallerID); }
/// <summary> /// On-premise Active Directory with Credentials /// </summary> /// <param name="apiUrl"></param> /// <param name="networkCredential"></param> public CRMWebAPI(string apiUrl, NetworkCredential networkCredential = null, Guid callerID = default(Guid)) { _crmWebAPIConfig = new CRMWebAPIConfig { APIUrl = apiUrl, NetworkCredential = networkCredential, CallerID = callerID }; if (_crmWebAPIConfig.NetworkCredential != null) { _httpClient = new HttpClient(new HttpClientHandler { Credentials = networkCredential }); } else { _httpClient = new HttpClient(); } SetHttpClientDefaults(callerID); }