Проблему удалось решить.
Оказалось, что пользователь, на которого зарегистрировано приложение в аксапте, должен иметь компанию, которая используется из консольного приложения, и обычный RetailUser не прокатил.
Заодно в процессе гугления я нашла другой способ работы с одатой - WEB API.
Достаточно неплохо описан, как для микрософта, есть куча примеров. Ссылки бросать не буду, но отлично гуглится по строке "Microsoft Dynamics 365 Web API"
Недостаток - как ни странно, запрос по определенному клиенту почему-то выдает не одного клиента, а всех. Похоже на баг.
Если кому надо, код очень простой:
Код:
//this is called from Page_load method, which is obviously not async:
//**** WEB API - unfortunately gives a list of customers instead of one, looks like a bug
AuthenticationResult token = OAuthHelper.GetAuthenticationHeader(true);
string queryOptions = "$select=Name,customerAccount";
string queryFilter = "$filter" + " dataAreaId eq '" + dataAreaId + "' and CustomerAccount eq '" + accountIdVal + "'";
string query = ODataEntityPath +"/"+ "Customers" + "?" + queryOptions + "&" + queryFilter;
_Default app = new _Default();
try
{
Task.WaitAll(Task.Run(async () => await app.SendRequestAsync(HttpMethod.Get, query, token)));
}
catch (System.Exception ex) { }
finally
{
if (app.httpClient != null)
{ app.httpClient.Dispose(); }
Console.WriteLine("Press <Enter> to exit the program.");
Console.ReadLine();
}
//********************
//this is async method
///<summary> Sends an HTTP request to the current service. </summary>
///<param name="method">The HTTP method to invoke</param>
///<param name="query">The HTTP query to execute (base URL is provided by client)</param>
///<param name="formatted">True to include formatted values in response; default is false.</param>
///<param name="maxPageSize">Number of records to display per output "page".</param>
///<returns>An HTTP response message</returns>
private async Task<HttpResponseMessage> SendRequestAsync(HttpMethod method, string query, AuthenticationResult _token, Boolean formatted = false, int maxPageSize = 10)
{
HttpResponseMessage response;
using (HttpClient httpClient = new HttpClient())
{
httpClient.Timeout = new TimeSpan(0, 2, 0); // 2 minutes
httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", _token.AccessToken);
HttpRequestMessage request = new HttpRequestMessage(method, query);
//request.Headers.Add("Prefer", "odata.maxpagesize=" + maxPageSize.ToString());
//if (formatted)
// request.Headers.Add("Prefer",
// "odata.include-annotations=OData.Community.Display.V1.FormattedValue");
response = await httpClient.SendAsync(request);
var responseBodyAsText = response.Content.ReadAsStringAsync();
}
return response;
}