1

如果很明显,我很抱歉,但我找不到答案。我想发布一个状态。我能找到的只有这两行。我不知道如何登录。我不在乎它是用户/密码还是使用 API 密钥。

var twitterCtx = new TwitterContext();
var tweet = twitterCtx.UpdateStatus(@"test text");

我将其作为控制台应用程序执行此操作。

4

1 回答 1

7

对不起我的英语,但我会尽力帮助你!

首先,您需要创建身份验证,一种方法是使用您自己的 Twitter 计数的用户和密码。

UsernamePasswordAuthorization cxauthentication = new UsernamePasswordAuthorization();
ctxauthenticatin.UserName = userName; // Put in your Twitter Account
ctxauthenticatin.Password = password; // and password
ctxauthenticatin.AllowUIPrompt = false;
ctxauthenticatin.SignOn();

var ctxTwitterContext = new TwitterContext(ctxauthentication);
ctxTwitterContext.UpdateStatus("test text"); 

还有另一种方法,您需要在此页面http://dev.twitter.com中在 twitter 中注册一个应用程序,在这里您注册一个输入名称的应用程序,您的网络方向,然后他们给您一个 ConsumerKey,ConsumerSecert如果您单击生成访问令牌,那么他们也会给您一个 AccessToken 和 AccessTokenSecret。记住转到设置并选择读取、写入和访问直接消息选项。然后生成AccessToken。好的,在您的代码中,您可以执行以下操作:

public partial class _Default : System.Web.UI.Page
{
private WebAuthorizer auth;
private TwitterContext twitterCtx;

protected void Page_Load(object sender, EventArgs e)
{
    IOAuthCredentials credentials = new SessionStateCredentials();

    if (credentials.ConsumerKey == null || credentials.ConsumerSecret == null)
    {
        credentials.ConsumerKey = "Here put your ConsumerKey";
        credentials.ConsumerSecret = "Here put your ConsumerSecret"
    }

    auth = new WebAuthorizer
    {
        Credentials = credentials,
        PerformRedirect = authUrl => Response.Redirect(authUrl)
    };
      if (!Page.IsPostBack)
    {
        auth.CompleteAuthorization(Request.Url);
    }
     twitterCtx = new TwitterContext(auth);
 }
protected void authorizeTwitterButton_Click(object sender, EventArgs e)
{
    auth.BeginAuthorization(Request.Url);
}

protected void SendTweet_Click(object sender, EventArgs e) { twitterCtx.UpdateStatus("My Test Tweet");
}

简单的!!好的,它是如何工作的!首先,当您单击按钮 authorizeTwitterButton 时,您开始为您的 twitter 帐户授权,并且新窗口将在登录 twitter 时打开,您授权应用程序,然后 twitter 使用必要的凭据重定向到您的页面,然后当您单击发送按钮时你发布了一条新推文!

还有另一种方式,您不需要使用开始和完成授权方法。这里直接介绍所有的凭证。例如:

var auth = new SingleUserAuthorizer
{
            Credentials = new InMemoryCredentials
            {
                ConsumerKey = "your ConsumerKey",
                ConsumerSecret = "Your consumerSecret",
                OAuthToken = "your AccessToken",
                AccessToken = "your AccessTokenSecret"]
            }
        };
var ctxTwitterContext = new TwitterContext(auth);
ctxTwitterContext.UpdateStatus("test text"); 

好的!希望我的回答对你有帮助!!有关更多信息,请访问http://linqtotwitter.codeplex.com/中的文档 再见!如果你喜欢我的回答,请给我一个点击!贾亚

于 2012-08-16T17:43:07.490 回答