hello Britney i had an application that posts tweets and images (using twitpic) and it was written using the C# library. Problem is the person who wrote the code wrote it a year ago before the twitter API v1.1 and my job is to update the application. and honestly its not working well for me!
so any help would be immensly appreciated.
im going to send you the previous code and the one im working on right now.
i would really be grateful if you could help me out.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TweetSharp;
using Hammock;
namespace TweetNow
{
class TweetManager
{
static public TwitterService initiate()
{
String _consumerKey = "******";
String _consumerSecret = "*******";
String _accessToken = "****";
String _accessTokenSecret = "******";
TwitterService service = new TwitterService(_consumerKey, _consumerSecret);
service.AuthenticateWith(_accessToken, _accessTokenSecret);
return service;
}
public static void sendTweet(String msg,String picname,TwitterService service) {
RestRequest request = service.PrepareEchoRequest();
request.Path = "uploadAndPost.xml";
request.AddFile("media", "smile", "Images/"+picname, "image/jpeg");
//request.AddField("key", "******");
request.AddField("key", "*****");
request.AddField("message", "Emote with a Pic!");
RestClient client = new RestClient { Authority = "http://api.twitpic.com/", VersionPath = "2"};
RestResponse response = client.Request(request);
Console.WriteLine(response.Content);
String str = response.Content;
int first = str.IndexOf("<url>");
int last = str.LastIndexOf("</url>");
string str2 = str.Substring(first + 5, last - first - 5);
service.SendTweet(msg+" "+str2+" .");
service.SendTweet(msg);
}
}
}
and heres the code i managed to write till now:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TweetSharp;
using Hammock;
namespace TweetNow
{
class TweetManager
{
static public TwitterService initiate()
{
var oauth_consumer_key = “xxxxxxx”;
var oauth_consumer_secret = “xxxx”;
var oauth_token = "479270385-Nda8X27zzi5xBKfUioNm9U8qCOzOvakVSGkS4YJS";
var oauth_token_secret = "xxxxx";
var oauth_version = "1.0";
var oauth_signature_method = "HMAC-SHA1";
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 = "http://api.twitter.com/1/statuses/update.json";
var status = " Updating status via REST API if this works";
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 (System.Security.Cryptography.HMACSHA1 hasher = new System.Security.Cryptography.HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey)))
{
oauth_signature = Convert.ToBase64String(hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString)));
}
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)
);
var postBody = "status=" + Uri.EscapeDataString(status);
System.Net.ServicePointManager.Expect100Continue = false;
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(resource_url);
request.Headers.Add("Authorization", authHeader);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
using (System.IO.Stream stream = request.GetRequestStream())
{
byte[] content = ASCIIEncoding.ASCII.GetBytes(postBody);
stream.Write(content, 0, content.Length);
}
System.Net.WebResponse response = request.GetResponse();
return service;
public static void sendTweet(String msg,String picname,TwitterService service) {
RestRequest request = service.PrepareEchoRequest();
request.Path = "uploadAndPost.xml";
request.AddFile("media", "smile", "Images/"+picname, "image/jpeg");
//request.AddField("key", "b9deee8ea5af8a6e1bac2aeb9d541c46");
request.AddField("key", "560adb8b7b328ae25ae3e7d3436de399");
request.AddField("message", "Emote with a Pic!");
RestClient client = new RestClient { Authority = "http://api.twitpic.com/", VersionPath = "2"};
RestResponse response = client.Request(request);
Console.WriteLine(response.Content);
String str = response.Content;
// int first = str.IndexOf("<url>");
//int last = str.LastIndexOf("</url>");
//string str2 = str.Substring(first + 5, last - first - 5);
// service.SendTweet(msg+" "+str2+" .");
service.SendTweet(msg);
}
}
}
i know i have a lot of errors but i found the solution online but i didnt quite understand it so im trying to get the pieces together…
thanks a million!