简体   繁体   English

Windows Phone中的Twitter帖子

[英]Twitter post in windows phone

I am working on an app where a user can login to their twitter account and post their status. 我正在开发一个应用程序,用户可以在其中登录其Twitter帐户并发布其状态。 Everything is going well but when I click on the tweet button to post, the status doesn't update. 一切进展顺利,但是当我单击tweet按钮发布时,状态不会更新。

What is wrong with my code? 我的代码有什么问题?

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));
}

Try this code instead of the last lines of your code 试试此代码,而不是代码的最后几行

    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));

I can't answer your question, but if you use API twitter api 1.0, move to api 1.1 right now. 我无法回答您的问题,但是如果您使用API​​ twitter api 1.0,请立即移至api 1.1。 They are going to retire api 1.0 next month: https://dev.twitter.com/blog/planning-for-api-v1-retirement . 他们将在下个月停用api 1.0: https//dev.twitter.com/blog/planning-for-api-v1-retirement Note, they are closing all but JSON, so your code might stop working at all at any moment. 请注意,它们将关闭除JSON之外的所有内容,因此您的代码可能会随时停止工作。

Hi Did you fine find the answer? 嗨,您找到答案了吗? If not please try with this: 如果没有,请尝试以下操作:

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"
    };

Here remove Version = "1.0" 在这里删除Version = "1.0"

So It will be look like this: 所以看起来像这样:

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,            
        };

Then change the path: 然后更改路径:

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

And finally you have to check one thing. 最后,您必须检查一件事。 Your Application Settings in https://apps.twitter.com/ 您在https://apps.twitter.com/中的应用程序设置

Change the Access level to Read, write, and direct messages (modify app permissions) 将访问级别更改为Read, write, and direct messages (modify app permissions)

If you can not change the permissions mean try from your mobile. 如果您无法更改权限,请尝试从您的手机上进行操作。 Go to this url from your mobile and log in with your user name and password. 从您的手机转到该URL,然后使用您的用户名和密码登录。 Now you can change the settings from mobile. 现在,您可以通过移动设备更改设置。 Now you can Post your twit..!! 现在,您可以发布您的twit .. !!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM