【问题标题】:OAuth With Twitter On C# .NET在 C# .NET 上使用 Twitter 进行 OAuth
【发布时间】:2014-03-14 01:34:04
【问题描述】:

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

问题是,我已经搜索了好几天,但我无法找到一个最新的和有效的示例供我学习。一切似乎都过时了。 (Twitterizer ; TweetSharp) 我已经从 Java 切换到 C# & .NET,但仍然没有进展。

我现在正在寻求直接帮助.. 编码 sn-ps .. 教程 .. 任何东西 .. 这样我就可以完成这项任务。我是 .NET 新手,但习惯于 Java 和 C/C++,所以我不希望被过于琐碎的代码弄糊涂..

我的确切要求是能够使用 twitter 登录、保存凭据(或用户的访问令牌等)并发布推文。

【问题讨论】:

  • LINQ to Twitter 并没有过时:linqtotwitter.codeplex.com
  • @L.B 你的目的是什么?
  • @JoeMayo 我意识到了。但我仍然无法实现代码。
  • @theonlyross 在可下载的源代码中有示例 - 查看标题为“Linq2TwitterDemos_”前缀的项目。
  • 我试过了,但无济于事。 This website 最终让我得到了我想要的。好吧,真的,因为我只能进行身份验证和登录。

标签: c# asp.net visual-studio-2010 twitter twitter-oauth


【解决方案1】:

这是一个关于如何验证和检索用户时间线的基本示例:

// 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<TwitAuthenticateResponse>(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示例):

Authenticate and request a user's timeline with Twitter API 1.1 oAuth

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

如果我要实现它,我会确切地找出需要通过 api 发布的内容,详情如下:

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

【讨论】:

  • 所有这些都进入PageLoad()函数?请记住,我是 C# 和 ASP .NET 的新手...
  • 我会将它包装在一个函数中,或者使用另一个问题的链接中的 nuget install。如果您是 asp .net 的新手,那么实施发布可能会很棘手。
  • 这是..我正在尝试我在this website找到的东西
  • 所以,这是一个旧答案,但万一其他人发现:此答案仅适用于 Application-only authentication。重要的部分是它不能为某人发推文。要为某人发推文,您必须使用另一个(更复杂的)答案。
【解决方案2】:

为了帮助其他与我陷入同样困境的人...查看this site

它将有助于验证、登录和检索基本信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-09
    • 2013-03-14
    • 2011-11-15
    • 1970-01-01
    • 2015-04-13
    • 2011-02-13
    • 2012-01-14
    • 2011-06-06
    相关资源
    最近更新 更多