如何使用ASP.NET MVC中的带有请求标头的HTTP get调用具有Oauth2授权的API?

时间:2019-05-02 01:18:20

标签: asp.net asp.net-mvc-4 oauth-2.0 http-headers

我很难执行带有oauth2授权的api标头的http get调用。

我已经尝试过下面的代码,但是随后我从api收到未经授权的响应。我认为问题在于,因为我执行了GETASYNC()而未添加一些标头。您能帮我找到执行GETASYNC()之前如何添加标头的方法吗?

public HttpClient webApiClient = new HttpClient();

public async System.Threading.Tasks.Task<ActionResult> Index()
{
    var uri = new Uri("https://myURL.com/"+ transno);

    ServicePointManager.Expect100Continue = true;
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

    var response = await webApiClient.GetAsync(uri);

    response.Headers.Add("Accept", "application/json");
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
    response.Headers.Add("client-id", "clientid");
    response.Headers.Add("client-secret", "clientsecret");
    response.Headers.Add("partner-id", "partnerid");   

    var result = JObject.Parse(await response.Content.ReadAsStringAsync());
}

0 个答案:

没有答案