0

I have an asp.net website which sends a tweet on a button click event.

I am using the TwitterApi for this and have an authenticated developer account.

This function was working from September 2018 until last month, but all of a sudden it won't send the tweets on request.

The response I get now is - "Id = 1, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}""

After searching around, this doesn't seem like a twitter error, but an async error of some kind. I have made some minor alterations to my website in this time, but I cant see anything I have changed that would cause this issue.

The code is below.

Can any one see why this error would occur?

protected void Publish_Click(object sender, EventArgs e)
    {
        DataAccess.Publish();

        SendEmails();
        SendTweet();

        Response.Redirect("OtherPage.aspx");
    }

public static void SendTweet()
    {
        string text = DataAccess.GetText();
        var twitter = new TwitterApi();
        var response = twitter.Tweet(text);
    }

public TwitterApi()
    {
        this.consumerKey = "XXX";
        this.consumerKeySecret = "XXX";
        this.accessToken = "XXX";
        this.accessTokenSecret = "XXX";

        sigHasher = new HMACSHA1(new ASCIIEncoding().GetBytes(string.Format("{0}&{1}", consumerKeySecret, accessTokenSecret)));
    }

public Task<string> Tweet(string text)
    {
        var data = new Dictionary<string, string> {
        { "status", text },
        { "trim_user", "1" }
    };

        return SendRequest("statuses/update.json", data);
    }

Task<string> SendRequest(string url, Dictionary<string, string> data)
    {
        var fullUrl = TwitterApiBaseUrl + url;

        // Timestamps are in seconds since 1/1/1970.
        var timestamp = (int)((DateTime.UtcNow - epochUtc).TotalSeconds);

        // Add all the OAuth headers we'll need to use when constructing the hash.
        data.Add("oauth_consumer_key", consumerKey);
        data.Add("oauth_signature_method", "HMAC-SHA1");
        data.Add("oauth_timestamp", timestamp.ToString());
        data.Add("oauth_nonce", "a"); // Required, but Twitter doesn't appear to use it, so "a" will do.
        data.Add("oauth_token", accessToken);
        data.Add("oauth_version", "1.0");

        // Generate the OAuth signature and add it to our payload.
        data.Add("oauth_signature", GenerateSignature(fullUrl, data));

        // Build the OAuth HTTP Header from the data.
        string oAuthHeader = GenerateOAuthHeader(data);

        // Build the form data (exclude OAuth stuff that's already in the header).
        var formData = new FormUrlEncodedContent(data.Where(kvp => !kvp.Key.StartsWith("oauth_")));

        return SendRequest(fullUrl, oAuthHeader, formData);
    }

async Task<string> SendRequest(string fullUrl, string oAuthHeader, FormUrlEncodedContent formData)
    {
        using (var http = new HttpClient())
        {
            http.DefaultRequestHeaders.Add("Authorization", oAuthHeader);

            var httpResp = await http.PostAsync(fullUrl, formData);
            var respBody = httpResp.ToString();

            return respBody;
        }
    }
EylM
  • 5,967
  • 2
  • 16
  • 28
user1948635
  • 1,357
  • 4
  • 15
  • 22
  • 3
    This isn't anything to do with Twitter, the problem is you have a bunch of async code but at no point do you await the results. You're also doing `httpResp.ToString()` which is wrong, instead you need to get the response correctly, for example `await httpResp.Content.ReadAsStringAsync()` – DavidG Aug 04 '19 at 22:12
  • I am a little unsure how this was even working before then. Unfortunately my backup has been replaced with this version so I cant see the changes. What could I amend above to get the async call / await to work? PS - I removed twitter as a tag but someone edited it back in. – user1948635 Aug 04 '19 at 23:02
  • I already gave you two things you need to do. – DavidG Aug 04 '19 at 23:22
  • 1
    Googling for the `WaitingForActivation` method finds answers similar/identical to the advice @DavidG gives. One of those answers even [right here on stack overflow](https://stackoverflow.com/questions/34551008/task-keeps-waiting-for-activation). – clarkitect Aug 04 '19 at 23:39
  • Possible duplicate of [Task keeps waiting for activation](https://stackoverflow.com/questions/34551008/task-keeps-waiting-for-activation) – Sir Rufo Aug 05 '19 at 00:16
  • Looking at the other questions in this area, it seems the general response is to make the click event async and make the page async="true". This was not the way it was setup before changes and it did work before, so there must be another solution – user1948635 Aug 05 '19 at 11:32

0 Answers0