· 7 years ago · Nov 30, 2017, 09:02 AM
1// the URL to obtain a temporary "request token"
2var rtUrl = "https://api.twitter.com/oauth/request_token";
3var oauth = new OAuth.Manager();
4// The consumer_{key,secret} are obtained via registration
5oauth["consumer_key"] = "~~~CONSUMER_KEY~~~~";
6oauth["consumer_secret"] = "~~~CONSUMER_SECRET~~~";
7oauth.AcquireRequestToken(rtUrl, "POST");
8var authzUrl = "https://api.twitter.com/oauth/authorize?oauth_token=" + oauth["token"];
9// here, should use a WebBrowser control.
10System.Diagnostics.Process.Start(authzUrl); // example only!
11// instruct the user to type in the PIN from that browser window
12var pin = "...";
13var atUrl = "https://api.twitter.com/oauth/access_token";
14oauth.AcquireAccessToken(atUrl, "POST", pin);
15
16// now, update twitter status using that access token
17var appUrl = "http://api.twitter.com/1/statuses/update.xml?status=Hello";
18var authzHeader = oauth.GenerateAuthzHeader(appUrl, "POST");
19var request = (HttpWebRequest)WebRequest.Create(appUrl);
20request.Method = "POST";
21request.PreAuthenticate = true;
22request.AllowWriteStreamBuffering = true;
23request.Headers.Add("Authorization", authzHeader);
24
25using (var response = (HttpWebResponse)request.GetResponse())
26{
27 if (response.StatusCode != HttpStatusCode.OK)
28 MessageBox.Show("There's been a problem trying to tweet:" +
29 Environment.NewLine +
30 response.StatusDescription);
31}