Windows phone 7: how to post tweet to twitter

2020-07-30 03:25发布

问题:

Currently i am working on sharing functionality from windows phone.

My purpose is to share the status of the user to Facebook and the Twitter from my windows phone App.

I completed the Facebook sharing successfully and now i m trying to share the status (120 Words tweet only) to twitter.

I completed the Authentication with Twitter account using this.

When i try to post the tweet to the twitter account after am logged in to the account using this tweet button click event,

 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.

回答1:

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.