I need to exclude twitter replies from my twitter feed - c#

Here is the code compliments of UmerPasha. It is returning tweets with replies. , but I got The code at UmerPasha blog. Thanks in advance. Im just including this paragraph so stack overflow will post.
public partial class _Default : System.Web.UI.Page
{
public string query = "";
// public string url = "https://api.twitter.com/1.1/users/search.json" ;
public string url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
protected void Page_Load(object sender, EventArgs e)
{
findUserTwitter(url, query); //problem
}
public void findUserTwitter(string resource_url, string q)
{
// oauth application keys
var oauth_token = ""; //"insert here...";
var oauth_token_secret = "; //"insert here...";
var oauth_consumer_key = "";// = "insert here...";
var oauth_consumer_secret = "";// = "insert here...";
// 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();
// 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}&q={6}";
var baseString = string.Format(baseFormat,
oauth_consumer_key,
oauth_nonce,
oauth_signature_method,
oauth_timestamp,
oauth_token,
oauth_version,
Uri.EscapeDataString(q) //problem
);
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)
);
ServicePointManager.Expect100Continue = false;
// make the request
var postBody = "q=" + Uri.EscapeDataString(q);//
resource_url += "?" + postBody;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url);
request.Headers.Add("Authorization", authHeader);
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";
var response = (HttpWebResponse)request.GetResponse();
var reader = new StreamReader(response.GetResponseStream());
var objText = reader.ReadToEnd();
myDiv.InnerHtml = objText;/**/
string html = "";
try
{
JArray jsonDat = JArray.Parse(objText);
for (int x = 0; x < jsonDat.Count(); x++)
{
//html += jsonDat[x]["id"].ToString() + "<br/>";
html += jsonDat[x]["text"].ToString() + "<br/>";
// html += jsonDat[x]["name"].ToString() + "<br/>";
html += jsonDat[x]["created_at"].ToString() + "<br/>";
}
myDiv.InnerHtml = html;
}
catch (Exception twit_error)
{
myDiv.InnerHtml = html + twit_error.ToString();
}
}
}

I was able to figure this out. In the Oauth Signature, the base format has to be in alphabetical order and also be included in the postBody.
var count = "5"; //*****************************************************************
var exclude_replies = "true"; //******************************************************************
var baseFormat = "count={7}&exclude_replies={8}&oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" + //************************
"&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&q={6}";
var baseString = string.Format(baseFormat,
oauth_consumer_key,
oauth_nonce,
oauth_signature_method,
oauth_timestamp,
oauth_token,
oauth_version,
Uri.EscapeDataString(q),
Uri.EscapeDataString(count), //*****************************************
Uri.EscapeDataString(exclude_replies)//*******************************************
);
baseString = string.Concat("GET&", Uri.EscapeDataString(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)
);
ServicePointManager.Expect100Continue = false;
// make the request
var postBody = string.Format("q={0}&count={1}&exclude_replies={2}", Uri.EscapeDataString(q),Uri.EscapeDataString(count),Uri.EscapeDataString(exclude_replies));//***
url += "?" + postBody;//
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);//
request.Headers.Add("Authorization", authHeader);
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";
var response = (HttpWebResponse)request.GetResponse();
var reader = new StreamReader(response.GetResponseStream());
var objText = reader.ReadToEnd();
myDiv.InnerHtml = objText;/**/
string html = "";
try
{
JArray jsonDat = JArray.Parse(objText);
for (int x = 0; x < jsonDat.Count(); x++)
{
//html += jsonDat[x]["id"].ToString() + "<br/>";
html += jsonDat[x]["text"].ToString() + "<br/>";
// html += jsonDat[x]["name"].ToString() + "<br/>";
//html += jsonDat[x]["created_at"].ToString() + "<br/>";
}
myDiv.InnerHtml = html;
}
catch (Exception twit_error)
{
myDiv.InnerHtml = html + twit_error.ToString();
}
}
}

Related

Twitter Send Direct Message C#

I am using the following code to send out message to one of my other ids. Can someone please explain what am I missing here. Both accounts are followers to each other and the option is also enabled to receive direct messages from everyone.
private static void sendMessage(string message)
{
//The facebook json url to update the status
string facebookURL = "https://api.twitter.com/1.1/direct_messages/new.json?user_id=1699575980&screen_name=MuneebZulfee&text=some message";
//set the access tokens (REQUIRED)
string oauth_consumer_key = "**************************";
string oauth_consumer_secret = "***************************";
string oauth_token = "******************************";
string oauth_token_secret = "*****************************";
// set the oauth version and signature method
string oauth_version = "1.0";
string oauth_signature_method = "HMAC-SHA1";
// create unique request details
string oauth_nonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
System.TimeSpan timeSpan = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc));
string oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();
// create oauth signature
string baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" + "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&screen_name={6}&text={7}&user_id={8}";
string baseString = string.Format(
baseFormat,
oauth_consumer_key,
oauth_nonce,
oauth_signature_method,
oauth_timestamp, oauth_token,
oauth_version,
Uri.EscapeDataString("MuneebZulfee"),
Uri.EscapeDataString("some message"),
Uri.EscapeDataString("1699575980")
);
string oauth_signature = null;
using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(Uri.EscapeDataString(oauth_consumer_secret) + "&" + Uri.EscapeDataString(oauth_token_secret))))
{
oauth_signature = Convert.ToBase64String(hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes("POST&" + Uri.EscapeDataString(facebookURL) + "&" + Uri.EscapeDataString(baseString))));
}
// create the request header
string authorizationFormat = "OAuth oauth_consumer_key=\"{0}\", oauth_nonce=\"{1}\", " + "oauth_signature=\"{2}\", oauth_signature_method=\"{3}\", " + "oauth_timestamp=\"{4}\", oauth_token=\"{5}\", " + "oauth_version=\"{6}\"";
string authorizationHeader = string.Format(
authorizationFormat,
Uri.EscapeDataString(oauth_consumer_key),
Uri.EscapeDataString(oauth_nonce),
Uri.EscapeDataString(oauth_signature),
Uri.EscapeDataString(oauth_signature_method),
Uri.EscapeDataString(oauth_timestamp),
Uri.EscapeDataString(oauth_token),
Uri.EscapeDataString(oauth_version)
);
HttpWebRequest objHttpWebRequest = (HttpWebRequest)WebRequest.Create(facebookURL);
objHttpWebRequest.Headers.Add("Authorization", authorizationHeader);
objHttpWebRequest.Method = "POST";
objHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
using (Stream objStream = objHttpWebRequest.GetRequestStream())
{
byte[] content = ASCIIEncoding.ASCII.GetBytes(Uri.EscapeDataString("user_id=1699575980&screen_name=MuneebZulfee&text=some message"));
objStream.Write(content, 0, content.Length);
}
var responseResult = "";
try
{
//success posting
WebResponse objWebResponse = objHttpWebRequest.GetResponse();
StreamReader objStreamReader = new StreamReader(objWebResponse.GetResponseStream());
responseResult = objStreamReader.ReadToEnd().ToString();
}
catch (Exception ex)
{
//throw exception error
responseResult = "Twitter Post Error: " + ex.Message.ToString() + ", authHeader: " + authorizationHeader;
}
}
Try using Tweet Sharp
var twitterApp = new TwitterService("******************", "****************");
twitterApp.AuthenticateWith("**********-*********************", "****************");
twitterApp.SendDirectMessage(new SendDirectMessageOptions() { ScreenName = "username", Text = DateTime.UtcNow.Ticks.ToString() });
This does for me

Trying to get twitter profile information in c#

I'm trying to retrieve profile data for an authenticated twitter user using the home_timeline API call. I've got a specific method used for this purpose and at this point I'm just trying to return the JSON string to verify it works, but I'm getting a 400 error.
public string getProfile(){
// set your own keys and screen name
var oauth_consumer_key = "consumerKeyHere";
var oAuthConsumerSecret = "consumerSecretHere";
var oAuthUrl = "https://api.twitter.com/1.1/statuses/home_timeline.json";
var oauth_token_secret = "tokenSecretHere";
// Authenticate
var authHeaderFormat = "Basic {0}";
var postBody = "grant_type=client_credentials";
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();
var oauth_token = token.access_token; // this token is generated by another class (token) where I retrieve the access token. Verified that authentication works.
// oauth implementation details
var oauth_version = "1.0";
var oauth_signature_method = "HMAC-SHA1";
// 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
);
baseString = string.Concat("GET&", Uri.EscapeDataString("https://api.twitter.com/1.1/statuses/home_timeline.json"), "&", Uri.EscapeDataString(baseString));
var compositeKey = string.Concat(Uri.EscapeDataString(oauth_consumer_key),
"&", 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_consumer_key=\"{0}\", oauth_nonce=\"{1}\", " +
"oauth_signature=\"{2}\", oauth_signature_method=\"{3}\", " +
"oauth_timestamp=\"{4}\", oauth_token=\"{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)
);
HttpWebRequest authRequest = (HttpWebRequest)WebRequest.Create(oAuthUrl);
authRequest.Headers.Add("Authorization", authHeader);
authRequest.Method = "GET";
authRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
authRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
WebResponse authResponse = authRequest.GetResponse();
// deserialize into an object
using (authResponse)
{
using (var reader = new StreamReader(authResponse.GetResponseStream())) {
String js = reader.ReadToEnd();
return js;
}
}
}
My arguments were in the wrong order for authHeader:
var headerFormat = "OAuth oauth_consumer_key=\"{0}\", oauth_nonce=\"{1}\", " +
"oauth_signature=\"{2}\", oauth_signature_method=\"{3}\", " +
"oauth_timestamp=\"{4}\", oauth_token=\"{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)
);

Need help about Search and Streaming Api of Twitter

I'm a newbie in C# and asp.net.
I'm trying to create a simple web(run on localhost) which use streaming and search API of Twitter.
I've tried some query like: "https://api.twitter.com/1.1/statuses/mentions_timeline.json?count=2&since_id=14927799" but it got some error about "Bad Authentication data".
May be I don't know how to get Authentication. So could anyone help me? THanks a lots.
What you have shown is not the search functionality, rather is the stream functionality.
Here is a complete example of working twitter stream search
private void SearchTweetsWithQuery(string query)
{
var oauth_token = System.Configuration.ConfigurationManager.AppSettings["accessToken"];
var oauth_token_secret = System.Configuration.ConfigurationManager.AppSettings["accessTokenSecret"];
var oauth_consumer_key = System.Configuration.ConfigurationManager.AppSettings["consumerKey"];
var oauth_consumer_secret = System.Configuration.ConfigurationManager.AppSettings["consumerSecret"];
// 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 if this works";
var resource_url = "https://api.twitter.com/1.1/search/tweets.json";
var screen_name = "TODO : UPDATE THIS OR LEAVE BLANK";
var baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" +
"&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&q={6}";
var baseString = string.Format(baseFormat,
oauth_consumer_key,
oauth_nonce,
oauth_signature_method,
oauth_timestamp,
oauth_token,
oauth_version,
query
);
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_consumer_key=\"{3}\", oauth_nonce=\"{0}\", oauth_signature=\"{5}\", oauth_signature_method=\"{1}\", " +
"oauth_timestamp=\"{2}\", " +
"oauth_token=\"{4}\", " +
"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
ServicePointManager.Expect100Continue = false;
var postBody = "screen_name=" + Uri.EscapeDataString(screen_name);
resource_url += "?q=" + query;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url);
request.Headers.Add("Authorization", authHeader);
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";
WebResponse response = request.GetResponse();
string responseData = new StreamReader(response.GetResponseStream()).ReadToEnd();
}
First you should create an application. https://dev.twitter.com/apps .
Then you can use Twitterizer's WebRequestBuilder class to form your request. ( http://svn.twitterizer.net/twitterizer/trunk/Twitterizer2/OAuth/ )
var tokens = new Twitterizer.OAuthTokens
{
ConsumerKey = "....",
ConsumerSecret = "....",
AccessToken = "....",
AccessTokenSecret = "......"
};
string url = "https://api.twitter.com/1.1/statuses/mentions_timeline.json?count=2&since_id=14927799";
var builder = new Twitterizer.WebRequestBuilder(new Uri(url), Twitterizer.HTTPVerb.GET, tokens);
var req = builder.PrepareRequest();
var resp = req.GetResponse();
var json = new StreamReader(resp.GetResponseStream()).ReadToEnd();
resp.Close();

Getting 401 from Twitter Stream API using C#

I got some code for Twitter update and search api from CodeProject which worked just fine.
However, i get 401 when i tried to convert it for the stream api: "https://stream.twitter.com/1/statuses/filter.json". I will appreciate any help. See code below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Security.Cryptography;
using System.IO;
public class OAuthRead
{
public static void Authenticate()
{
// oauth application keys
var oauth_consumer_key = "****";
var oauth_consumer_secret = "*****";
var oauth_token = "******";
var oauth_token_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();
//var resource_url = "https://stream.twitter.com/1/statuses/filter.json?track=obama&include_entities=1&language=en";
//var resource_url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
var resource_url = "https://stream.twitter.com/1.1/statuses/filter.json";
//var screen_name = "femitfash";
// 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(screen_name)
);
baseString = string.Concat("POST&", 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
ServicePointManager.Expect100Continue = false;
var postBody = "track=obama&include_entities=1&language=en"; // "screen_name=" + Uri.EscapeDataString(screen_name);//
resource_url += "?" + postBody;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url);
request.Headers.Add("Authorization", authHeader);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.PreAuthenticate = true;
request.AllowWriteStreamBuffering = true;
WebResponse response = request.GetResponse();
string responseData = new StreamReader(response.GetResponseStream()).ReadToEnd();
}
}
try include your query string to baseString:
var baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" +
"&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&track={6}";
var baseString = string.Format(baseFormat,
oauth_consumer_key,
oauth_nonce,
oauth_signature_method,
oauth_timestamp,
oauth_token,
oauth_version,
Uri.EscapeDataString(valueToTrack)
);
....
var postBody = "track=obama";
https://dev.twitter.com/docs/auth/creating-signature says:
5. Percent encode the parameter string and append it to the output string.

Error 'UnAuthorized' when using the "GET" requests for Twiiter API 1.1 in C#

I'm trying to connect to twitter api from my c# application using the "GET" requests. I read the following article:
http://www.codeproject.com/Articles/247336/Twitter-OAuth-authentication-using-Net
Which works great with me and I'm able to post a tweet successfully. But When I try to use it with "GET" and the Twitter API 1.1, it keeps giving me the error 'UnAuthorized'
This is my code:
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 Id = "111111111111111";// the ID of the tweet need to get info about it
var resource_url = "https://api.twitter.com/1.1/statuses/show.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}&status={6}";
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("POST&", 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)
);
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url + "?id=" + Id);
request.Headers.Add("Authorization", authHeader);
request.Method = "GET";// "POST";
request.ContentType = "application/x-www-form-urlencoded";
WebResponse response = request.GetResponse();
StreamReader s = new StreamReader(response.GetResponseStream(), encode);
string test = s.ReadLine();
Can you help me of how to edit it to be working with "GET"?
Thank you,

Categories