Controller class
public string strTwiterFollowers { get; set; }
public ActionResult FollowerlistTwitter()
{ //
////// application keys for application --- My_Twitter_integration_1
var oauth_token = "";
var oauth_token_secret = "";
var oauth_consumer_key = "";
var oauth_consumer_secret = "";
// oauth implementation details
var oauth_version = "1.0";
var oauth_signature_method = "HMAC-SHA1";
// unique request details
var oauth_nonce = Convert.ToBase64String(
new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
var timeSpan = DateTime.UtcNow
- new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
var oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();
// message api details
//var status = "Updating status via REST API 1.1";
var resource_url = "https://api.twitter.com/1.1/followers/list.json";
// create oauth signature
var baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" +
"&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}";
var baseString = string.Format(baseFormat,
oauth_consumer_key,
oauth_nonce,
oauth_signature_method,
oauth_timestamp,
oauth_token,
oauth_version
//,Uri.EscapeDataString(status)
);
baseString = string.Concat("GET&", Uri.EscapeDataString(resource_url), "&", Uri.EscapeDataString(baseString));
var compositeKey = string.Concat(Uri.EscapeDataString(oauth_consumer_secret),
"&", Uri.EscapeDataString(oauth_token_secret));
string oauth_signature;
using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey)))
{
oauth_signature = Convert.ToBase64String(
hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString)));
}
// create the request header
var headerFormat = "OAuth oauth_nonce=\"{0}\", oauth_signature_method=\"{1}\", " +
"oauth_timestamp=\"{2}\", oauth_consumer_key=\"{3}\", " +
"oauth_token=\"{4}\", oauth_signature=\"{5}\", " +
"oauth_version=\"{6}\"";
var authHeader = string.Format(headerFormat,
Uri.EscapeDataString(oauth_nonce),
Uri.EscapeDataString(oauth_signature_method),
Uri.EscapeDataString(oauth_timestamp),
Uri.EscapeDataString(oauth_consumer_key),
Uri.EscapeDataString(oauth_token),
Uri.EscapeDataString(oauth_signature),
Uri.EscapeDataString(oauth_version)
);
// make the request
// var postBody = "status=" + Uri.EscapeDataString(status);
ServicePointManager.Expect100Continue = false;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url);
request.Headers.Add("Authorization", authHeader);
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";
//using (Stream stream = request.GetRequestStream())
//{
// byte[] content = ASCIIEncoding.ASCII.GetBytes(postBody);
// stream.Write(content, 0, content.Length);
//}
WebResponse response = request.GetResponse();
string result = new StreamReader(response.GetResponseStream()).ReadToEnd();
JObject j = JObject.Parse(result);
JArray data = (JArray)j["users"];
int totalFollowers = (data).Count;
ViewBag.lblTotalFollowers = "laxyo_soft has " + (data).Count + " Followers";
if (data != null)
{
List<TwiterFollower> listFollowers = new List<TwiterFollower>();
foreach (var item in data)
{
TwiterFollower objTwiterFollowers = new TwiterFollower();
objTwiterFollowers.ScreenName = item["screen_name"].ToString().Replace("\"", "");
objTwiterFollowers.ProfileImage = item["profile_image_url"].ToString().Replace("\"", "");
objTwiterFollowers.UserId = item["id"].ToString().Replace("\"", "");
listFollowers.Add(objTwiterFollowers);
}
Random objRnd = new Random();
List<TwiterFollower> rendomFollowers = listFollowers.OrderBy(item => objRnd.Next()).ToList<TwiterFollower>();
for (int i = 0; i <= 3; i++)
{
strTwiterFollowers = strTwiterFollowers + "<li><a target='_blank' title='" + rendomFollowers[i].ScreenName + "' href=https://twitter.com/" + rendomFollowers[i].ScreenName + "><img src='" + rendomFollowers[i].ProfileImage + "'/><span>" + rendomFollowers[i].ScreenName + "</span></a></li>";
}
}
return View("Index");
}
<div style="float: left;" class="twiteer-widget">
<img src="https://twimg0-a.akamaihd.net/profile_images/1696202368/305410_2367904889198_1597723971_2270173_1865831839_n_bigger.jpg" />
<div class="follow-btn">
<a href="https://twitter.com/laxyo_soft ">laxyo_soft </a>
<a href="https://twitter.com/laxyo_soft " class="twitter-follow-button" data-show-count="false"
data-lang="en">Follow@twitterapi</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
</div>
@*<asp:Label ID="lblTotalFollowers" runat="server"></asp:Label>*@
<ul>
@*<%=strTwiterFollowers%>*@
@ViewBag.lblTotalFollowers
</ul>
</div>
In Model class
public class TwiterFollower
{
public string ScreenName { get; set; }
public string ProfileImage { get; set; }
public string UserId { get; set; }
}
Can you answer this question?
Answer
0 Answers