Twitter anywhere Tweet Box Json response

523 Views Asked by At

First of all, here is my basic code:

    <div id="example-ontweet"></div>

<script type="text/javascript">

  twttr.anywhere(function (T) {

    T("#example-ontweet").tweetBox({

      onTweet : function(plaintext, html) {

        alert(plaintext);
        alert(html);

      }

    });

  });

</script>

When I send I submit a tweet, it works fine like it should. When I try to submit a duplicate tweet, it doesn't allow it, and the loading spinner just spins with no error message--also default behavior. When I look in the firebug console upon duplicate tweet submit, I see:

POST https://api.twitter.com/1/statuses/update.json 403 forbidden

and under the Response console tab in there is this info:

{"error":"Status is a duplicate.","request":"\/1\/statuses\/update.json"}

All I want to do is alert that response message on fail--either the whole object, or preferably just the "error." I tried:

me.tweetBox.$button.click(function(){   
    $.getJSON('https://api.twitter.com/1/statuses/update.json', function(data) {
        alert(JSON.stringify(data));
    });
});

Both inside of a settimeout function and not, and it looked like it was going to work, but it spit this out below into the console when it tried:

GET https://api.twitter.com/1/statuses/update.json    401 Unauthorized

Any ideas of how to pull that error message into an alert? It seems like it should be so simple, but I'm banging my head against the wall. Thanks

2

There are 2 best solutions below

3
Musa On

Try

me.tweetBox.$button.click(function(){   
    $.getJSON('https://api.twitter.com/1/statuses/update.json', function(data) {
        alert(JSON.stringify(data));
    }).error(function(jqXHR, textStatus, errorThrown){
        alert(jqXHR.responseText);
    });
});
0
Patriotec On

I believe the new changes to Twitter's API doesn't allow duplicate messages.

https://dev.twitter.com/docs/api/1.1/post/statuses/update

For each update attempt, the update text is compared with the authenticating user's recent tweets. Any attempt that would result in duplication will be blocked, resulting in a 403 error. Therefore, a user cannot submit the same status twice in a row.