简体   繁体   English

Windows Phone 7:如何将推文发布到Twitter

[英]Windows phone 7: how to post tweet to twitter

Currently i am working on sharing functionality from windows phone. 目前,我正在从Windows Phone共享功能。

My purpose is to share the status of the user to Facebook and the Twitter from my windows phone App. 我的目的是从Windows Phone应用程序向Facebook和Twitter分享用户的状态。

I completed the Facebook sharing successfully and now im trying to share the status (120 Words tweet only) to twitter. 我成功完成了Facebook共享,现在我试图将状态(仅120 Words tweet)共享给Twitter。

I completed the Authentication with Twitter account using this . 我使用this完成了Twitter帐户的身份验证。

When i try to post the tweet to the twitter account after am logged in to the account using this tweet button click event, 当我尝试使用此tweet按钮单击事件登录到帐户后,将tweet发布到twitter帐户时,

 private void btnPostTweet_Click(object sender, RoutedEventArgs e)
    {
        var credentials = new OAuthCredentials
        {
            Type = OAuthType.ProtectedResource,
            SignatureMethod = OAuthSignatureMethod.HmacSha1,
            ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
            ConsumerKey = AppSettings.consumerKey,
            ConsumerSecret = AppSettings.consumerKeySecret,
            Token = this.accessToken,
            TokenSecret = this.accessTokenSecret,
            Version = "1.1"
        };

        var restClient = new RestClient
        {
            Authority = "https://api.twitter.com",
            HasElevatedPermissions = true
        };

        var restRequest = new RestRequest
        {
            Credentials = credentials,
            Path = "/1.1/statuses/update.json",
            Method = WebMethod.Post
        };

  restRequest.AddParameter("status", Uri.EscapeDataString(txtTweetContent.Text));

        restClient.BeginRequest(restRequest, new RestCallback(PostTweetRequestCallback));
 }

And in callback, 在回调中

private void PostTweetRequestCallback(RestRequest request, RestResponse response, object obj)
    {
        string str = response.ToString();
        Deployment.Current.Dispatcher.BeginInvoke(() =>
        {
            if (response.StatusCode == HttpStatusCode.OK)
            {
                MessageBox.Show(AppSettings.TWEET_POSTED_SUCCESSFULLY);
            }
            else if (response.StatusCode == HttpStatusCode.Forbidden)
            {
                MessageBox.Show(AppSettings.TWEET_POST_ERR_UPDATE_LIMIT);
            }
            else
            {
                MessageBox.Show(AppSettings.TWEET_POST_ERR_FAILED);
            }
            txtTweetContent.Text = "";
        });
    }

it gives me error as 它给了我错误

"Bad Authentication data", Code="215"

I've successfully registered my Application to twitter developer account and Received the Access Token keys before making this call. 我已经成功地将我的应用程序注册到twitter开发人员帐户,并在进行此调用之前已收到访问令牌密钥。

From analyzing your code its seems you have to change at below code block : 通过分析您的代码,看来您必须在下面的代码块中进行更改:

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

Change this : 改变这个:

 Version = "1.1" to Version="1.0"

Now your code will works smoothly, enjoy the day. 现在,您的代码将顺利运行,祝您愉快。

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

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