开发者

How to RETWEET, REPLY or add a tweet to Favorite?

开发者 https://www.devze.com 2023-03-04 08:54 出处:网络
I am trying to implement retweet and reply functionality in my site in sharepoint 2010. I am creating a web part and trying to fetch all the tweets for particular hash tags.

I am trying to implement retweet and reply functionality in my site in sharepoint 2010. I am creating a web part and trying to fetch all the tweets for particular hash tags.

I am able to get the Hash tag data but here I have to开发者_StackOverflow社区 put Retweet, Reply and Favorite buttons on every tweet. I am trying for retweet right now and my javascript code looks like this:

$.getJSON("http://search.twitter.com/search.json?q=%23" + hashtag + "&rpp="+ nooftweets +"&&callback=?", function (msg) {
            container.html(''); //Remove the Loading GIF
            for (i = 0; i < msg.results.length; i++) { //Build DIVs containing Tweets and add it to Container DIV
                var str = '<div class=\'tweet\'><div class=\'avatar\'><img src="' + msg.results[i].profile_image_url + '" alt=\'twitter-img\'/></div>';
                str += '<div class=\'status-body\'><a href="http://twitter.com/' + msg.results[i].from_user + '"target="_blank">' + msg.results[i].from_user + '</a>';
                str += '<div>' + formatTwitString(msg.results[i].text) + '</div></div>';
                str += '<div class=\'created_at\'>' + relativeTime(msg.results[i].created_at) + '</div>';
                str += '**<div><a href="http://api.twitter.com/1/statuses/retweets/' + msg.results[i].id_str + '" target=_blank>Retweet</a></div>**</div>';
                container.append(str);
            }

But my retweet code is not working properly.. Please help me in rectifying the problem.

Thanks in advance.


Able to do it now.. I have added some line of code in my getJSON. Now it is working fine.. It is redirecting the user to twitter. If the user is logged in then it won't ask the credential else it will open the twitter login page.

$.getJSON("http://search.twitter.com/search.json?q=" + repValue + "&rpp="+ nooftweetslength +"&&callback=?", function(msg) { for (i = 0; i < msg.results.length; i++) { //Build DIVs containing Tweets and add it to Container DIV str = ''; str += '' + msg.results[i].from_user + ''; str += '' + formatTwitString(msg.results[i].text) + ''; str += '' + relativeTime(msg.results[i].created_at) + ''; str += 'Reply '; str += ' Retweet '; str += ' Favorite'; container.append(str); } });


Use Web Intents if you have access to the tweetId. This works in my case.

Ref: http://dev.twitter.com/pages/intents

Hope it helps!


  1. Where's container defined?
  2. Since it's html in text:

    container.innerHTML = str;

  3. Use double quotes instead of single quotes so you don't need to do all that escaping

  4. Consider using JQuery or another library to construct elements instead of all the HTML shown. It makes it easier to read.
  5. Be sure the format of msg is actually a parsed object before working with it.


Assuming the user has already authenticated your app, according to http://dev.twitter.com/doc/post/statuses/retweet/:id you need to POST the retweet request. Clicking a link (even with target="_blank") will issue a GET request.

I suggest using $.post to send the retweet request.

0

精彩评论

暂无评论...
验证码 换一张
取 消