【问题标题】:ASP.NET C# curl call with OAuth not working带有 OAuth 的 ASP.NET C# curl 调用不起作用
【发布时间】:2019-01-15 00:17:10
【问题描述】:

我正在尝试使用 OAuth 1 对新的雅虎天气 api 进行此 curl 调用,如下所示:

[HttpGet]
public HttpResponseMessage getWeather()
{

    using (HttpClient httpClient = new HttpClient())
    {

        httpClient.BaseAddress = new Uri("http://weather-ydn-yql.media.yahoo.com/");

        httpClient.DefaultRequestHeaders.Accept.Clear();

        httpClient.DefaultRequestHeaders.Add("Authorization", "OAuth oauth_consumer_key = \"(MY CONSUMER KEY)\", oauth_signature_method = \"HMAC-SHA1\", oauth_timestamp = \"1547473450\", oauth_nonce = \"Ll7ArdU1yN0\", oauth_version = \"1.0\", oauth_signature = \"(MY GENERATED SIGNATURE FROM POSTMAN)\"");

        //httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("OAuth", "oauth_consumer_key = \"(MY CONSUMER KEY)\", oauth_signature_method = \"HMAC-SHA1\", oauth_timestamp = \"1547472939\", oauth_nonce = \"vu3HE92s6A3\", oauth_version = \"1.0\", oauth_signature = \"(MY GENERATED SIGNATURE FROM POSTMAN)\"");

        HttpResponseMessage response = httpClient.GetAsync("forecastrss?location=hamilton&format=json").Result;

        return response;
    }
}

但是当我运行它时,它会返回这个错误:

请提供有效的凭据。身份验证 oauth_problem="OST_OAUTH_PARAMETER_ABSENT_ERROR", 领域="yahooapis.com"

看来我的 OAuth 参数丢失了。我的问题是如何使用 OAuth 1 身份验证在 ASP.NET C# 中进行 curl?

【问题讨论】:

    标签: c# asp.net oauth


    【解决方案1】:

    使用 RestClient 解决了这个问题

    var client = new RestClient("http://weather-ydn-yql.media.yahoo.com/forecastrss?location=hamilton&format=xml");
    var request = new RestRequest(Method.GET);
    request.AddHeader("Postman-Token", "ac0c256b-e727-4b01-b4fe-edd8b7d7073a");
    request.AddHeader("cache-control", "no-cache");
    request.AddHeader("Authorization", "OAuth oauth_consumer_key="(MY CONSUMER KEY)",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1547481203",oauth_nonce="304ixaov43G",oauth_version="1.0",oauth_signature="(MY GENERATED SIGNATURE FROM POSTMAN)"");
    IRestResponse response = client.Execute(request);
    

    【讨论】:

      猜你喜欢
      • 2013-11-14
      • 2018-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-23
      • 1970-01-01
      • 2015-07-08
      相关资源
      最近更新 更多