首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >调用其他外部API的.Net核心Web

调用其他外部API的.Net核心Web
EN

Stack Overflow用户
提问于 2021-03-28 20:32:44
回答 1查看 3.6K关注 0票数 0

我正在创建一个.Net核心web,它将进一步调用一个外部API。

场景:

在.Net核心web中,我想向UiPath Orchestrator发出Post请求以获得身份验证令牌。下面是我要创建https://postman.uipath.rocks/#8f3b38d8-57dc-4d79-876d-0d2ff897f61a的请求的链接

代码语言:javascript
运行
复制
[HttpPost]
public Task<IActionResult> GetAccessToken()
{
  //Here I want to make a Post request to the API to Authenticate
}

响应(8月令牌)将发送到另一个post请求,以启动进程https://postman.uipath.rocks/#738c3beb-1c19-4257-8474-841a054c4220

代码语言:javascript
运行
复制
[HttpPost]
 public Task<IActionResult> StartProcess(string token)
 {
   //Here I want to make a Post request to the other API in which token from the previous call will be 
    sent in header
 }

我必须在一两天内完成它,我不知道如何才能做到这一点。

任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2021-03-28 21:50:57

如果我正确理解,您希望有一个端点,它将向第三方API发出两个POST请求。第一个请求将检索授权令牌。第二个请求将在其标头中使用令牌来传递授权。

要实现这种行为,您应该在API中使用一个端点,而不是两个端点(如上面的示例所示)。

另外,我建议您有一个单独的类,它将负责与第三方API (与postman.uipath.rocks)进行通信。

因此,调用第三方API的单个端点可能如下所示:

代码语言:javascript
运行
复制
public class Controller
{
    // another Controller's code...
    private readonly IPostmanApi postmanApi;

    public Controller(IPostmanApi postmanApi)
    {
        this.postmanApi = postmanApi;
    }

    [HttpPost]
    public async Task<IActionResult> StartPostmanProcess()
    {
        string token = await postmanApi.GetToken();
        await postmanApi.StartProcess(token);

        return Accepted();
    }

    // another Controller's code...
}

您的IPostmanApi.cs文件可能如下所示:

代码语言:javascript
运行
复制
public interface IPostmanApi
{
    ///<summary>
    /// A method to get Authorization Token from Postman
    ///</summary>
    Task<string> GetToken();

    ///<summary>
    /// A method to get start a process in Postman
    ///</summary>
    ///<param name="token">Postman's authorization token</param>
    Task StartProcess(string token);
}

您的PostmanApi.cs文件可能如下所示:

代码语言:javascript
运行
复制
public class PostmanApi
{
    Task<string> GetToken()
    {
        using (HttpClient client = new HttpClient())
        {
            var response = await client.PostAsync("https://postman.uipath.rocks/#8f3b38d8-57dc-4d79-876d-0d2ff897f61a");

            // Note: this code assumes that the whole response body is a string Token.
            // If the `postman.uipath.rocks` returns an JSON object, which contains a
            // Token inside some field, you may use Newtonsoft.Json library to deserialize
            // it, and then retrieve your Token from corresponding field.
            // See example here: https://stackoverflow.com/questions/24131067/deserialize-json-to-array-or-list-with-httpclient-readasasync-using-net-4-0-ta
            return await response.Content.ReadAsStringAsync();
        }
    }

    Task StartProcess(string token)
    {
        using (HttpClient client = new HttpClient())
        {
            // Note: this code assumes that the `postman.uipath.rocks` receives
            // a **Bearer** token. If it receives another kind of authentication
            // scheme, you should use it instead of "Bearer".
            client.DefaultRequestHeaders.Authorization 
                         = new AuthenticationHeaderValue("Bearer", token);
            await client.PostAsync("https://postman.uipath.rocks/#738c3beb-1c19-4257-8474-841a054c4220");
    }
}

另外,不要忘记在IPostmanApi中注册Startup.cs服务。

代码语言:javascript
运行
复制
public void ConfigureServices(IServiceCollection services)
{
    // another code...

    services.AddScoped<IPostmanApi, PostmanApi>();
    // another code...
}

公告:

  1. 这段代码不是线程安全的(因为HttpClient不是线程安全的)。如果您希望它是线程安全的,请考虑使用IHttpClientFactory

有用的链接:

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66845936

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档