Mike Upjohn
Mike Upjohn

Reputation: 1297

The certificate key algorithm is not supported with Twitter API

We're pulling the latest tweets from a twitter timeline, but I'm seeing an error "The certificate key algorithm is not supported." with a message "The underlying connection was closed: An unexpected error occurred on a receive.". This only happens on stage and live servers not on my development machine.

The page returns a standard server error. The class we call with the screenname is below. We've double checked the oAuth consumer key and secret are correct as well.

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>();
    }

The error occurs at WebResponse authResponse = authRequest.GetResponse(); but I've no idea how to fix this.

Adding the following to web.config has no bearing on the result

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

Any advice here?

Thanks!

Upvotes: 1

Views: 1368

Answers (2)

Mike Upjohn
Mike Upjohn

Reputation: 1297

We fixed it - I don't think we will ever find out what caused it, but we completed wiped the DLL's that were compiled for the solution, and rebuilt in a new folder on the server and it worked. I guess a line of code somewhere in the project was breaking it, but will never know what unfortunately.

We did check the time on the servers, but everything seemed to be OK there.

Upvotes: 0

ade
ade

Reputation: 36

Have you checked the time on the servers that are failing? You can sometimes see requests failing if the servers time is a few minutes off from the time on the twitter servers. Make sure everything is synced with NTP and see if that works.

Upvotes: 1

Related Questions