在C#.NET上使用Twitter进行OAuth

简单地说,我正在尝试使用Visual Studio 2012创建一个网页,其中用户将登录一个页面,该页面在获得授权后将发布推文,并使他们能够使用页面上的其他function。 我想记住所说的用户凭据还限制他们使用某个function的速率。

问题是,我一直在寻找几天,我无法找到最新的日期和工作示例供我遵循。 一切似乎都已经过时了。 ( TweetSharp ; TweetSharp )我已经从Java切换到C#和.NET,但仍然没有进展。

我现在正在寻求直接帮助..代码片段..教程..任何事情……这样我就可以完成这项任务。 我是.NET新手,但习惯于使用Java和C / C ++,所以我不希望被过于琐碎的代码所迷惑。

我的确切要求是能够使用Twitter登录,保存凭据(或用户的访问令牌,无论如何)并发布推文。

这是如何validation和检索用户时间轴的基本示例:

 // You need to set your own keys and screen name var oAuthConsumerKey = "superSecretKey"; var oAuthConsumerSecret = "superSecretSecret"; var oAuthUrl = "https://api.twitter.com/oauth2/token"; var screenname = "aScreenName"; // Do the Authenticate var authHeaderFormat = "Basic {0}"; var authHeader = string.Format(authHeaderFormat, Convert.ToBase64String(Encoding.UTF8.GetBytes(Uri.EscapeDataString(oAuthConsumerKey) + ":" + Uri.EscapeDataString((oAuthConsumerSecret))) )); var postBody = "grant_type=client_credentials"; HttpWebRequest authRequest = (HttpWebRequest)WebRequest.Create(oAuthUrl); authRequest.Headers.Add("Authorization", authHeader); authRequest.Method = "POST"; authRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8"; authRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; using (Stream stream = authRequest.GetRequestStream()) { byte[] content = ASCIIEncoding.ASCII.GetBytes(postBody); stream.Write(content, 0, content.Length); } authRequest.Headers.Add("Accept-Encoding", "gzip"); WebResponse authResponse = authRequest.GetResponse(); // deserialize into an object TwitAuthenticateResponse twitAuthResponse; using (authResponse) { using (var reader = new StreamReader(authResponse.GetResponseStream())) { JavaScriptSerializer js = new JavaScriptSerializer(); var objectText = reader.ReadToEnd(); twitAuthResponse = JsonConvert.DeserializeObject(objectText); } } // Do the timeline var timelineFormat = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name={0}&include_rts=1&exclude_replies=1&count=5"; var timelineUrl = string.Format(timelineFormat, screenname); HttpWebRequest timeLineRequest = (HttpWebRequest)WebRequest.Create(timelineUrl); var timelineHeaderFormat = "{0} {1}"; timeLineRequest.Headers.Add("Authorization", string.Format(timelineHeaderFormat, twitAuthResponse.token_type, twitAuthResponse.access_token)); timeLineRequest.Method = "Get"; WebResponse timeLineResponse = timeLineRequest.GetResponse(); var timeLineJson = string.Empty; using (timeLineResponse) { using (var reader = new StreamReader(timeLineResponse.GetResponseStream())) { timeLineJson = reader.ReadToEnd(); } } public class TwitAuthenticateResponse { public string token_type { get; set; } public string access_token { get; set; } } 

请看这里(github项目有asp.net和asp.net mvc示例):

使用Twitter API 1.1 oAuthvalidation并请求用户的时间表

我为它创建了一个开源项目,但不幸的是它还没有发送推文的能力。 我认为这不会很难,但我现在的时间很短。

如果我要实现它,我会找出api需要发布的确切内容,详见此处:

https://dev.twitter.com/docs/api/1.1/post/statuses/update

为了帮助那些陷入同样差距的人我做了…查看这个网站

它将有助于validation和登录和检索基本信息。