0

我正在开发一个应用程序,用户可以登录到他们的 Twitter 帐户并发布他们的状态。一切都很顺利,但是当我点击推文按钮发布时,状态没有更新。

我的代码有什么问题?

private void btnPostTweet_Click(object sender, RoutedEventArgs e)
{
    if (txtBoxNewTweet.Text.Trim().Length == 0) { return; }

    var credentials = new OAuthCredentials
    {
        Type = OAuthType.ProtectedResource,
        SignatureMethod = OAuthSignatureMethod.HmacSha1,
        ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
        ConsumerKey = TwitterSettings.consumerKey,
        ConsumerSecret = TwitterSettings.consumerKeySecret,
        Token = this.accessToken,
        TokenSecret = this.accessTokenSecret,
        Version = "1.0"
    };

    var restClient = new RestClient
    {
        Authority = TwitterSettings.StatusUpdateUrl,
        HasElevatedPermissions = true,
        Credentials = credentials,
        Method = WebMethod.Post
    };

    restClient.AddHeader("Content-Type", "application/x-www-form-urlencoded");

    // Create a Rest Request and fire it
    var restRequest = new RestRequest
    {
        Path = "1/statuses/update.xml?status=" + txtBoxNewTweet.Text 
    };

    var ByteData = Encoding.UTF8.GetBytes(txtBoxNewTweet.Text);
    restRequest.AddPostContent(ByteData);
    restClient.BeginRequest(restRequest, new RestCallback(PostTweetRequestCallback));
}
4

3 回答 3

0

试试这段代码,而不是代码的最后几行

    restRequest = new RestRequest
    {
        Path = "1/statuses/update.xml"
    };
    restRequest.AddParameter("status", tbxNewTweet.Text);

    //var ByteData = Encoding.UTF8.GetBytes(txtBoxNewTweet.Text);
    //restRequest.AddPostContent(ByteData);
    restClient.BeginRequest(restRequest, new RestCallback(PostTweetRequestCallback));
于 2013-02-26T09:58:59.523 回答
0

我无法回答您的问题,但如果您使用 API twitter api 1.0,请立即移至 api 1.1。他们将在下个月淘汰 api 1.0:https ://dev.twitter.com/blog/planning-for-api-v1-retirement 。请注意,他们正在关闭除 JSON 之外的所有内容,因此您的代码可能随时停止工作。

于 2013-02-26T10:37:59.957 回答
0

嗨,你找到答案了吗?如果没有,请尝试以下操作:

var credentials = new OAuthCredentials
    {
        Type = OAuthType.ProtectedResource,
        SignatureMethod = OAuthSignatureMethod.HmacSha1,
        ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
        ConsumerKey = TwitterSettings.consumerKey,
        ConsumerSecret = TwitterSettings.consumerKeySecret,
        Token = this.accessToken,
        TokenSecret = this.accessTokenSecret,
        Version = "1.0"
    };

这里删除 Version = "1.0"

所以它看起来像这样:

var credentials = new OAuthCredentials
        {
            Type = OAuthType.ProtectedResource,
            SignatureMethod = OAuthSignatureMethod.HmacSha1,
            ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
            ConsumerKey = TwitterSettings.consumerKey,
            ConsumerSecret = TwitterSettings.consumerKeySecret,
            Token = this.accessToken,
            TokenSecret = this.accessTokenSecret,            
        };

然后改变路径:

var restRequest = new RestRequest
    {
        Path = "1.1/statuses/update.xml?status=" + txtBoxNewTweet.Text 
    };

最后你必须检查一件事。您在https://apps.twitter.com/中的应用程序设置

将访问级别更改为Read, write, and direct messages (modify app permissions)

如果您无法更改权限,请从您的手机尝试。从您的手机转到此网址并使用您的用户名和密码登录。现在您可以从移动设备更改设置。现在你可以发布你的推特了..!!

于 2014-09-20T10:27:11.567 回答