如何使用.Net Core 2.2 AddHttpClient获取要分配给我的HttpClient的OAuth令牌?

时间:2019-06-11 11:43:07

标签: c# authorization asp.net-authorization auth-token httpclientfactory

我正在编写一个Asp.Net Core 2.2应用程序,其中添加了HttpClient。

rosrun dji_sdk_demo demo_mission

因此,基本上,我为自己编写了一个扩展程序,该扩展程序允许我使用最少的配置来进行AddQuickbooksOnline身份验证,因为该扩展程序默认情况下会进行许多配置。

注册并验证我的应用后,我将向已验证的用户显示一个GetProducts按钮。

我希望此按钮调用我的ProductController。

        services.AddAuthentication(o => {
            o.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            o.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            o.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        })
            .AddQuickbooksOnline(o => {
                o.ClientId = Configuration["ecm.qbo.client-id"];
                o.ClientSecret = Configuration["ecm.qbo.client-secret"];
                o.Scope.Add(QuickbooksOnlineScopes.Accounting);
            });

        services.AddHttpClient("qbo", c => {
            c.BaseAddress = new Uri("https://sandbox-quickbooks.api.intuit.com");
            c.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            c.DefaultRequestHeaders.Add("content-type", "application/json");
            c.DefaultRequestHeaders.Authorization=// How do I get the token to be assigned here?
        });

        service.AddScoped<IInventoryService, QboInventoryService>();

该服务实际上取决于我要在服务配置中添加的已配置HttpClient。

public class ProductController : ControllerBase {
    private readonly IInventoryService _inventoryService;
    public ProductController(IInventoryService inventoryService) {
        _inventoryService=inventoryService??throw new ArgumentNullException(nameof(inventoryService));
    }

    public async Task<IEnumerable<Product>> Get() {
        var products=await _inventoryService.GetAllProducts();
        return products;
    }
}

因此,为了调用QBO WebAPI,我需要向已实例化的HttpClient提供经过身份验证的用户的OAuth令牌。

0 个答案:

没有答案