如何使用这些 cURL 命令调用用于访问令牌的 REST Web API - 在 C# 中



我对OAuth有点陌生,但正在学习。 我所拥有的是我正在尝试遵循的本教程。 我正在尝试遵循"生物 - 检索传记记录数据"下的简单 3 行示例:https://members.orcid.org/api/tutorial-retrieve-data-public-api-curl-12-and-earlier

不过,我需要做的是使用 C# 控制台程序而不是 CURL,我想使用 HttpClient 命名空间。 我有自己的客户端 ID 和客户端密钥可供使用。 我正在尝试使用此公共 API 获取访问令牌以存储在任何地方,然后像此示例一样调用以获取 XML 数据。 我的问题是我不知道如何将他们的 CURL 调用转换为 C# 代码。 我没有为此使用 ASP.NET 网站...这一切都需要在 C# 控制台程序中完成。

如果您

计划将 curl 用于许多不同的任务,而不是重写工具。只需下载 Windows 兼容版本并将其作为资源嵌入到控制台应用程序中即可。

Windows(32位)版本的curl.exe - https://akamai.bintray.com/5a/5aa976c1cfc96d95a12cb469ce9bbb1b193ae7599d446f4a22d023a585c5ffe9?gda=exp=1455730084~hmac=85d7d80271ecba52893dacb956690dedd020c0ad61f2943b28adcc43ce14e45a&response-content-disposition=attachment%3Bfilename%3D%22curl-7.47.1-win32-mingw.7z%22&response-content-type=application%2Fx-www-form-urlencoded

编辑 - 像这样:

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "C:CURLcurl.exe";
p.StartInfo.Arguments = "-i -L -H 'Accept: application/json' -d 'client_id=APP-GWTDOE3CX89FH7FD' -d 'client_secret=5160c539-4ec9-4e8c-8966-580df68b494f' -d 'scope=/read-public' -d 'grant_type=client_credentials' 'https://pub.sandbox.orcid.org/oauth/token'";           
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine(output);

我在RestSharp上取得了成功。当存在这么多好的库时,使用原始的 HttpClent 似乎是一种浪费。

最新更新