雅虎,OAuth http请求。适用于localhost但不适用于实时服务器

时间:2011-08-23 16:34:58

标签: asp.net httpwebrequest yahoo

我有这个代码试图通过Yahoo!进行身份验证它在我的本地服务器上工作正常,但在我的实时服务器上发出的相同请求失败。当它调用GetResponse()时,它会返回(401)Unauthorized错误。谁能帮我理解为什么?如果您需要更多信息,请与我们联系。

try
{
    string url = "https://api.login.yahoo.com/oauth/v2/get_request_token?oauth_callback=" + Server.UrlEncode("http://www.dowdlefolkart.com/extensions/contacts/webform1.aspx");
    url = GetUrl(url, consumerKey, consumerSecret);

    var req = System.Net.HttpWebRequest.Create(url);
    using (var res = req.GetResponse().GetResponseStream())
    {
        ....
    }
}
catch (Exception ex)
{
    Response.Write("ERROR" + ex.Message);
}

以下是完整的错误异常:

System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.Net.WebException: The remote server returned an error: (401) Unauthorized. 
at System.Net.HttpWebRequest.GetResponse() 
at NopSolutions.NopCommerce.Web.Extensions.Contacts.WebForm1.Page_Load(Object sender, EventArgs e) 
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) 
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) 
at System.Web.UI.Control.OnLoad(EventArgs e) 
at System.Web.UI.Control.LoadRecursive() 
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 
at System.Web.UI.Page.HandleError(Exception e) 
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 
at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 
at System.Web.UI.Page.ProcessRequest() 
at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) 
at System.Web.UI.Page.ProcessRequest(HttpContext context) 
at ASP.extensions_contacts_webform1_aspx.ProcessRequest(HttpContext context) 
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

更新

我收到了Fiddler的错误。

oauth_problem=timestamp_refused&oauth_acceptable_timestamps=1314119105-1314120305

所以看起来我的实时服务器的时间戳不正确。这就是我创建时间戳的方式:

string timestamp = Math.Floor((DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds).ToString();

1 个答案:

答案 0 :(得分:1)

我发现了这个问题。出错的原因是时间戳无效。它比允许的略大。使用Fiddler我能够确定这个问题。错误是:oauth_problem=timestamp_refused&oauth_acceptable_timestamps=1314119105-1314120305

然后我登录到我的实时服务器并将系统时钟调整到需要的状态。显然快了15分钟。一旦我调整了这个错误就消失了。谢谢@Darin Dimitrov的帮助。