Unable to send updates to Twitter using the following code. No error
I tried the following code:
static void PostTweetanother(string username, string password, string tweet)
{
try
{
// encode the username/password
string user = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + password));
// determine what we want to upload as a status
byte[] bytes = System.Text.Encoding.ASCII.GetBytes("status=" + tweet);
// connect with the update page
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://twitter.com/statuses/update.xml");
// set the method to POST
request.Method = "POST";
request.ServicePoint.Expect100Continue = false; // thanks to argodev for this recent change!
// set the authorisation levels
request.Headers.Add("Authorization", "Basic " + user);
开发者_JAVA技巧 request.ContentType = "application/x-www-form-urlencoded";
// set the length of the content
request.ContentLength = bytes.Length;
// set up the stream
Stream reqStream = request.GetRequestStream();
// write to the stream
reqStream.Write(bytes, 0, bytes.Length);
// close the stream
reqStream.Close();
}
catch (Exception ex) { throw ex; }
}
I also tried this code:
static void PostTweet(string username, string password, string tweet)
{
// Create a webclient with the twitter account credentials, which will be used to set the HTTP header for basic authentication
WebClient client = new WebClient { Credentials = new NetworkCredential { UserName = username, Password = password } };
// Don't wait to receive a 100 Continue HTTP response from the server before sending out the message body
ServicePointManager.Expect100Continue = false;
// Construct the message body
byte[] messageBody = Encoding.ASCII.GetBytes("status=" + tweet);
// Send the HTTP headers and message body (a.k.a. Post the data)
client.UploadData("http://twitter.com/statuses/update.xml", messageBody);
}
There are a few problems here. First, the Twitter REST API no longer supports basic authentication; you must use OAuth now. Second, you are referencing the wrong endpoint; you should be calling statuses/update, like so http://api.twitter.com/1/statuses/update.xml
(although I always recommend using JSON over XML when interacting with the Twitter API.)
Since you must authenticate to post to Twitter, and since you must use OAuth, the final thing I would recommend is using a Twitter library like Twitterizer to interact with the Twitter API.
Twitter access is pretty much a commodity these days, and there are many good libraries out there (that you don't have to maintain.) What you are trying to do: post a status update, distills to the following simplified code (example uses Twitterizer):
//reference Twitterizer2.dll
var tokens = new Twitterizer.OAuthTokens {
AccessToken = @"myAccessToken",
AccessTokenSecret = @"myAccessTokenSecret",
ConsumerKey = @"myConsumerKey",
ConsumerSecret = @"myConsumerSecret"
};
// Post the update to twitter
var statusResponse = Twitterizer.TwitterStatus.Update(tokens,
"I am your status update!");
if (statusResponse.Result != Twitterizer.RequestResult.Success)
return;
// Fetch the authenticated user's timeline, uses statuses/user_timeline
// under the hood
var timelineResponse = Twitterizer.TwitterTimeline.UserTimeline(tokens);
if (timelineResponse.Result != Twitterizer.RequestResult.Success)
return;
foreach (var status in timelineResponse.ResponseObject)
{
Console.WriteLine(status.Text);
}
In order to get your OAuth credentials, you'll need to register your application under your account at https://dev.twitter.com/apps/new. Once that's done, you can find your consumer key and secret on the main page for your newly-created application, and your access token and secret will be on the page linked under the My Access Token menu item on the right of the page.
Using this one set of credentials will work fine if you have just the one user, and what the code example illustrates is called the single access token pattern. If, however, you want to allow other folks to use your application under their own identities, then you'll need to perform what's known as the "OAuth dance" in order to acquire access tokens for each of your users.
More information about the full OAuth flow is available at http://dev.twitter.com/auth, and token exchange is introduced specifically here.
Recently, I have read on the Internet that twitter have closed their Rest API for third party clients, so it could be you can't post updates due to this fact. That's my opinion, so I think it's impossible to do what you want to do.
See you!
精彩评论