【问题标题】:The certificate key algorithm is not supported with Twitter APITwitter API 不支持证书密钥算法
【发布时间】:2016-01-28 16:59:17
【问题描述】:

我们正在从 Twitter 时间线中提取最新的推文,但我看到一个错误“不支持证书密钥算法”。带有消息“底层连接已关闭:接收时发生意外错误。”。这只发生在舞台和实时服务器上,而不是我的开发机器上。

页面返回标准服务器错误。我们用屏幕名称调用的类如下。我们已经仔细检查了 oAuth 消费者密钥和秘密是否正确。

public static List<Tweet> GetTimeline(string screenName)
{
    try
    {
        // Do the Authenticate
        var authHeaderFormat = "Basic {0}";
        var authHeader = string.Format(authHeaderFormat,
                                           Convert.ToBase64String(Encoding.UTF8.GetBytes(Uri.EscapeDataString(oAuthConsumerKey) + ":" + Uri.EscapeDataString((oAuthConsumerSecret)))

                                               ));
        var postBody = "grant_type=client_credentials";
        HttpWebRequest authRequest = (HttpWebRequest)WebRequest.Create(oAuthUrl);

        authRequest.Headers.Add("Authorization", authHeader);
        authRequest.Method = "POST";
        authRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
        authRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
        using (Stream stream = authRequest.GetRequestStream())
        {
            byte[] content = ASCIIEncoding.ASCII.GetBytes(postBody);
            stream.Write(content, 0, content.Length);
        }
        authRequest.Headers.Add("Accept-Encoding", "gzip");
        WebResponse authResponse = authRequest.GetResponse();
        // deserialize into an object
        TwitAuthenticateResponse twitAuthResponse;
        using (authResponse)
        {
            using (var reader = new StreamReader(authResponse.GetResponseStream()))
            {
                JavaScriptSerializer js = new JavaScriptSerializer();
                var objectText = reader.ReadToEnd();
                twitAuthResponse = JsonConvert.DeserializeObject<TwitAuthenticateResponse>(objectText);
            }
        }

        // Do the timeline
        var timelineFormat =
                "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name={0}&include_rts=1&exclude_replies=1&count=20";
        var timelineUrl = string.Format(timelineFormat, screenName);
        HttpWebRequest timeLineRequest = (HttpWebRequest)WebRequest.Create(timelineUrl);
        var timelineHeaderFormat = "{0} {1}";
        timeLineRequest.Headers.Add("Authorization", string.Format(timelineHeaderFormat, twitAuthResponse.token_type, twitAuthResponse.access_token));
        timeLineRequest.Method = "Get";
        WebResponse timeLineResponse = timeLineRequest.GetResponse();

        var timeLineJson = string.Empty;
        using (authResponse)
        {
            using (var reader = new StreamReader(timeLineResponse.GetResponseStream()))
            {
                timeLineJson = reader.ReadToEnd();
            }
        }

        JArray tweets = JArray.Parse(timeLineJson);

        return (from t in tweets
            select new Tweet
            {
                date = DateTime.ParseExact((string)t["created_at"], "ddd MMM dd HH:mm:ss +ffff yyyy", new System.Globalization.CultureInfo("en-US")),
                link = t["entities"]["urls"].Count() > 0 ? (string)t["entities"]["urls"][0]["url"] : "",
                tweet = LinkUpTweet((string)t["text"]),
                author = (string)t["user"]["name"],
                screenname = (string)t["user"]["screen_name"],
                tweetLink = string.Format(@"https://twitter.com/{0}/status/{1}", screenName, (string)t["id_str"])
            }).ToList();
    }
    catch (Exception ex)
    {
        // Send Email Error
        return new List<Tweet>();
    }

错误发生在WebResponse authResponse = authRequest.GetResponse();,但我不知道如何解决。

将以下内容添加到 web.config 对结果没有影响

<system.diagnostics>
   <switches>
       <add name="System.Net" value="0"/>
   </switches>
</system.diagnostics>

这里有什么建议吗?

谢谢!

【问题讨论】:

  • 简单问题:您是否尝试过在您的 Staging 和 Prod 服务器主机上的网络浏览器中访问您发布的 URL?我问是因为您的问题听起来像是低级证书握手错误,而不是与 oAuth/Basic Auth 握手有关。
  • 这篇文章表明 system.diagnotics 部分可能会导致这个问题stackoverflow.com/questions/30576179/…
  • 我在服务器上检查过,只是看到一个通用的内部服务器错误。我的 web.config 中也没有 system.diagnostics 部分并将其添加进去,没有区别。我也读过那篇文章,但没有快乐。

标签: c# twitter twitter-oauth httpwebresponse x509certificate2


【解决方案1】:

您是否检查过出现故障的服务器上的时间?如果服务器时间与 twitter 服务器上的时间相差几分钟,您有时会看到请求失败。确保一切都与 NTP 同步,看看是否有效。

【讨论】:

    【解决方案2】:

    我们修复了它 - 我认为我们永远不会找出导致它的原因,但我们完成了为解决方案编译的 DLL 的擦除,并在服务器上的新文件夹中重建并且它工作正常。我猜项目中某处的一行代码破坏了它,但不幸的是永远不知道是什么。

    我们确实检查了服务器上的时间,但那里似乎一切正常。

    【讨论】:

      猜你喜欢
      • 2017-07-21
      • 1970-01-01
      • 1970-01-01
      • 2016-11-03
      • 2013-12-09
      • 2017-09-01
      • 2016-07-22
      • 2018-04-05
      • 1970-01-01
      相关资源
      最近更新 更多