Afternoon Matt
Below is the C# code snippet.
Using a specific users credentials, I can access and display their tweets.
As soon as I add a different screen name & count, I get a 401 Unauthorised error
Even if I add in the screen name of the user whose credentials I’ve authenticated with, I get a 401 Unauthorised error
Any help would be appreciated?
This has been mentioned at various
class Program
{
static void Main(string[] args)
{
var oauth_token = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
var oauth_token_secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
var oauth_consumer_key = "XXXXXXXXXXXXXXXXXX";
var oauth_consumer_secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
var maxRetries = 5;
var retries = 0;
var submitted = false;
string twitterTime = null;
while (!submitted && retries < maxRetries)
{
try
{
//Request details
var oauth_version = "1.0";
var oauth_signature_method = "HMAC-SHA1";
var nonceValue = Guid.NewGuid().ToString().Replace("-", string.Empty);
var oauth_nonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
var baseTime = (retries == 0)
? DateTime.UtcNow
: DateTime.ParseExact(twitterTime, "ddd, dd MMM yyyy HH:mm:ss zzz", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
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://api.twitter.com/1.1/statuses/user_timeline.json";
var screenName = "screenname";
var count = "2";
//GS - When building the signature string the params
//must be in alphabetical order. I can't be bothered
//with that, get SortedDictionary to do it's thing
SortedDictionary<string, string> sd =
new SortedDictionary<string, string>();
sd.Add("count", count);
sd.Add("screen_name", screenName);
sd.Add("oauth_version", oauth_version);
sd.Add("oauth_consumer_key", oauth_consumer_key);
sd.Add("oauth_nonce", oauth_nonce);
sd.Add("oauth_signature_method", oauth_signature_method);
sd.Add("oauth_timestamp", oauth_timestamp);
sd.Add("oauth_token", oauth_token);
//GS - Build the signature string
string baseString = String.Empty;
baseString += "GET" + "&";
baseString += Uri.EscapeDataString(resource_url) + "&";
foreach (KeyValuePair<string, string> entry in sd)
{
baseString += Uri.EscapeDataString(entry.Key) +
Uri.EscapeDataString("=") + Uri.EscapeDataString(entry.Value) + Uri.EscapeDataString("&");
}
//GS - Remove the trailing ambersand char, remember
//it's been urlEncoded so you have to remove the
//last 3 chars - %26
baseString = baseString.Substring(0, baseString.Length - 3);
//Encrypt data
var compositeKey = string.Concat(Uri.EscapeDataString(oauth_consumer_secret),
"&", Uri.EscapeDataString(oauth_token_secret));
string oauth_signature;
using (HMACSHA1 hasher = new HMACSHA1(new ASCIIEncoding().GetBytes(compositeKey)))
{
oauth_signature = Convert.ToBase64String(hasher.ComputeHash(new ASCIIEncoding().GetBytes(baseString)));
}
//Finish Authentication 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_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)
);
//Disable expect 100 continue header
ServicePointManager.Expect100Continue = false;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url);
request.Headers.Add("Authorization", authHeader);
request.Method = "GET";
request.Timeout = 3 * 60 * 100;
//request.KeepAlive = false;
WebResponse response = request.GetResponse();
string responseData;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
responseData = reader.ReadToEnd();
JavaScriptSerializer jss = new JavaScriptSerializer();
List<Tweet> tweets = jss.Deserialize<List<Tweet>>(responseData);
//var sw = File.CreateText("C:\\temp\\twitterfeed.txt");
//sw.WriteLine(responseData);
//Console.WriteLine("File created successfully");
var counter = 1;
foreach (var tweet in tweets)
{
if (counter <= 3)
{
Console.WriteLine(string.Format("Created at: {0}", tweet.Created_At));
Console.WriteLine(string.Format("Text: {0}", tweet.Text ));
}
counter++;
}
Console.ReadLine();
}
submitted = true;
Console.ReadLine();
}
catch (WebException ex)
{
if (ex.Response != null)
{
twitterTime = ex.Response.Headers["Date"].Replace(" UTC", " +0000");
}
else
{
Console.WriteLine("amount of retries: " + retries);
Console.WriteLine(ex.ToString());
Console.ReadLine();
}
}
catch (FormatException fe)
{
Console.WriteLine(fe.ToString());
Console.ReadLine();
}
finally
{
retries++;
}
//return responseData; // Or do whatever you want with the response
}
Console.WriteLine("amount of retries: " + retries);
Console.ReadLine();
}
}