【发布时间】:2021-05-11 03:57:38
【问题描述】:
我正在寻找一种简单的方法来连接到 .NET 控制台应用程序(不涉及浏览器)中的 Azure Odata 数据存储(特别是 Dynamics365 for Finance and Operations)并检索 odata json。我搜索了所有 MS 文档,终于找到了这个:
public OAuthMessageHandler(string serviceUrl, string clientId, string redirectUrl, string username, string password, HttpMessageHandler innerHandler): base(innerHandler) {
AuthenticationParameters ap = AuthenticationParameters.CreateFromResourceUrlAsync(new Uri(serviceUrl + "/data/")).Result;
AuthenticationContext authContext = new AuthenticationContext(ap.Authority, false);
AuthenticationResult authResult;
if (username != string.Empty && password != string.Empty) {
UserCredential cred = new UserCredential(username, password);
authResult = authContext.AcquireToken(serviceUrl, clientId, cred);
} else {
authResult = authContext.AcquireToken(serviceUrl, clientId, new Uri(redirectUrl), PromptBehavior.Auto);
}
authHeader = new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
}
下面的 try/catch 在我的控制台应用程序的静态 main 方法中,并调用上面的函数:
try {
messageHandler = new OAuthMessageHandler(serviceUrl, clientId, string.Empty, userName, password, new HttpClientHandler());
using(HttpClient client = new HttpClient(messageHandler)) {
client.BaseAddress = new Uri(serviceUrl);
client.Timeout = new TimeSpan(0, 2, 0); //2 minutes
client.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
client.DefaultRequestHeaders.Add("OData-Version", "4.0");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = client.GetAsync("api/data/v9.0/WhoAmI", HttpCompletionOption.ResponseHeadersRead).Result;
if (response.IsSuccessStatusCode) {
//Get the response content and parse it.
JObject body = JObject.Parse(response.Content.ReadAsStringAsync().Result);
Guid userId = (Guid) body["UserId"];
Console.WriteLine("Your system user ID is: {0}", userId);
} else {
throw new Exception(response.ReasonPhrase);
}
}
} catch (Exception ex) {
DisplayException(ex);
}
重要提示:我可以使用我拥有的用户名和密码在浏览器中成功连接到 D365 F&O。我将它们传递给上面的函数。我也有一个有效的clientId。我知道所有这些都是有效的,因为我有一个第 3 方应用程序,通过 usn、pwd、clientId 和授权 URL,我能够连接 F&O 及其 odata 并与之交互。但我试图在我的 .NET 控制台应用程序中尽可能简单地复制这种交互。
我从 this approach 开始,但最终偏离并混合了来自其他文档的示例和方法,只是为了编译。
以下是我的详细信息,尽可能不混淆:
D365 F&O URL: https://mycustomer2492efd6bdevaos.cloudax.dynamics.com/
Odata URL: https://mycustomer2492efd6bdevaos.cloudax.dynamics.com/data/InventItemPrices
Client Id: c0bzae2e-5233-4465-a2f2-ceb87aa52c3f (I changed some chars)
Authorization URL: https://login.windows.net/mycustomer.com
我有用户名和密码。我不需要redirectUrl,所以我将其保留为空字符串。我也不需要客户机密。 (如果我有客户端密码,这一切都会简单得多,因为我找到了更简单的 c# 代码示例来处理它。)
当我调用上面的函数时,我得到了这个错误:
验证标头格式无效 - 参数名称: 验证头
在这一行抛出异常:
AuthenticationParameters ap = AuthenticationParameters.CreateFromResourceUrlAsync(new Uri(serviceUrl + "/data/")).Result;
我不喜欢我正在使用的方法,这只是我发现的所有实际编译和尝试工作的方法。我将非常感谢具有尽可能少的依赖项的自包含函数来连接到我的 odata 并根据需要拉下 json。
【问题讨论】:
-
您是否有理由需要通过 SDK 使用 OData 端点?
-
blockingHD:完全没有。宁愿不使用 SDK。除了标准 .NET 之外,没有依赖项的原始 c# 会很棒。
-
URI 需要看起来像这样 'private static string serviceUrl = yourorg.crm.dynamics.com'
-
jdweng:不,这不是 crm。这是 F&O。
标签: c# azure oauth-2.0 odata microsoft-dynamics