如何使用C#.NET从我的ASP.NET页面更新Twitter状态?

时间:2011-11-10 08:20:53

标签: c# asp.net twitter twitter-oauth twitterizer

这是我的按钮点击代码,但状态未在Twitter中更新。请帮帮我。

protected void btnTwitt_Click(object sender, EventArgs e)
{
    string twitterMsg = txtShout.Text;
    OAuthTokens tokens = new OAuthTokens();
    tokens.AccessToken = "xxxxx";
    tokens.AccessTokenSecret = "yyyy";
    tokens.ConsumerKey = "tttt";
    tokens.ConsumerSecret = "hhhh";
    TwitterResponse<Twitterizer.TwitterStatus> tweetResponse = 
    Twitterizer.TwitterStatus.Update(tokens, twitterMsg);
    lblTwitMsg.Text = "Your have shout successfully on http://twitter.com/" + "";
}

4 个答案:

答案 0 :(得分:1)

我认为您关注此网站http://www.twitterizer.net/

请按照以下链接获取详细信息..我还没有尝试,但这些都有关于实施的详细信息..

http://www.dougv.com/2009/07/01/posting-status-updates-tweets-to-a-twitter-profile-via-asp-net/

http://dotnetguts.blogspot.com/2010/05/tweet-posting-from-aspnet-using-oauth.html

你为什么不试试这个?这是推特小部件代码,这也将做同样的事情..你必须登录一次然后你能够发布/分享它...

<a href="http://twitter.com/share" class="twitter-share-button"
data-url=""
data-text="testing tweet post" data-count="none" data-via="niranjankala" data-counturl="">
Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js">
</script>

它正在运作.. enter image description here

希望这有帮助..

答案 1 :(得分:0)

"tttt"的某个地方,您需要输入consumer key"hhhh",您需要插入secret key。我认为您已更新其他更新内容。或者你可以从这里得到一个https://dev.twitter.com/apps/new

以下是您可以复制并粘贴的完整代码,因为我已经完成了vb,您可以从此处使用C#转换器进行转换:C# Converter

首先从http://www.twitterizer.net/downloads/下载twitterizer.dll添加一个新的类文件,我将其命名为(BasPage.vb)

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Data
Imports System.Diagnostics
Imports Twitterizer

Public Class BasePage
    Inherits System.Web.UI.Page

    Protected Sub DisplayAlert(ByVal msg As String)
        ClientScript.RegisterStartupScript(Me.GetType(), Guid.NewGuid().ToString(), String.Format("alert('{0}');", msg.Replace("'", "\'").Replace(Constants.vbCrLf, "\n")), True)
    End Sub

    Protected Function GetCachedAccessToken() As OAuthTokens
        If Session("AccessToken") IsNot Nothing Then
            Return CType(Session("AccessToken"), OAuthTokens)
        Else
            Return Nothing
        End If
    End Function

    Public Function GetCachedUserId() As ULong
        If Session("GetCachedUserId") IsNot Nothing Then
            Return Convert.ToUInt64(Session("GetCachedUserId"))
        Else
            Return ULong.MinValue
        End If
    End Function

    Protected Sub CreateCachedAccessToken(ByVal requestToken As String)
        Dim ConsumerKey As String = ConfigurationManager.AppSettings("ConsumerKey")
        Dim ConsumerSecret As String = ConfigurationManager.AppSettings("ConsumerSecret")

        Dim responseToken As OAuthTokenResponse = OAuthUtility.GetAccessToken(ConsumerKey, ConsumerSecret, requestToken)

        'Cache the UserId
        Session("GetCachedUserId") = responseToken.UserId

        Dim accessToken As New OAuthTokens()
        accessToken.AccessToken = responseToken.Token
        accessToken.AccessTokenSecret = responseToken.TokenSecret
        accessToken.ConsumerKey = ConsumerKey
        accessToken.ConsumerSecret = ConsumerSecret

        Session("AccessToken") = accessToken
    End Sub

    Protected Function GetTwitterAuthorizationUrl() As String
        Dim ConsumerKey As String = ConfigurationManager.AppSettings("ConsumerKey")
        Dim ConsumerSecret As String = ConfigurationManager.AppSettings("ConsumerSecret")

        Dim reqToken As OAuthTokenResponse = OAuthUtility.GetRequestToken(ConsumerKey, ConsumerSecret)
        Return "https://twitter.com/oauth/authorize?oauth_token=" & reqToken.Token
    End Function
End Class

然后添加一个按钮并粘贴此代码:

 Protected Sub ibSignInWithTwitter_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibSignInWithTwitter.Click
        Response.Redirect(MyBase.GetTwitterAuthorizationUrl())
  End Sub

最后在你的web.config文件中你需要添加:

        <add key="ConsumerKey" value="you consumer key"/>
        <add key="ConsumerSecret" value="you consumersecret key"/>
        <add key="Twitterizer2.EnableStatisticsCollection" value="false"/>

如果您有任何问题请告诉我。希望它可以帮助您。

答案 2 :(得分:0)

为了确定您的代码有什么问题,您应该检查结果状态。

protected void btnTwitt_Click(object sender, EventArgs e)
{
     string twitterMsg = txtShout.Text;

    OAuthTokens tokens = new OAuthTokens();
    tokens.AccessToken = "xxxxx";
    tokens.AccessTokenSecret = "yyyy";
    tokens.ConsumerKey = "tttt";
    tokens.ConsumerSecret = "hhhh";
    TwitterResponse<Twitterizer.TwitterStatus> tweetResponse = Twitterizer.TwitterStatus.Update(tokens, twitterMsg);
    if (tweetResponse.Result == RequestResult.Success)
    {
        lblTwitMsg.Text = "Your have shout successfully on http://twitter.com/";
    }
    else
    {
        lblTwitMsg.Text = string.format("The tweet could not be posted: {0}", tweetResponse.ErrorMessage);
    }
}

tweetResponse.Result属性也会让您知道发生了什么。

如果您还有其他问题,请在论坛上联系Twitterizer支持(实际上是我):http://forums.twitterizer.net/

答案 3 :(得分:-1)

 try getting oauth token first using this.sorry if it doesnot help ..   

var consumerKey = ConfigurationManager.AppSettings["consumerKey"];
     var consumerSecret = ConfigurationManager.AppSettings["consumerSecret"];
     //Step 1: Get Request Token
      string callbackAddress = "http://xxx.co/folder/Twitter.aspx";
     OAuthTokenResponse RequestToken = OAuthUtility.GetRequestToken(consumerKey, consumerSecret, callbackAddress);
    //  Step 2: Redirect User to Requested Token
            Response.Redirect("http://twitter.com/oauth/authorize?oauth_token=" + RequestToken.Token);