简体   繁体   中英

Windows phone 7: how to post tweet to twitter

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 im 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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