How to update twitter status using C # and LINQ to Twitter libraries

I am writing a Metrol Style app to update status on my Twitter. I am using LINQ to Twitter library. But I don’t understand why my application throws 401 Unauthorized exception. Here is my code:

private void UpdateStatus() { // configure the OAuth object var auth = new SingleUserAuthorizer { Credentials = new InMemoryCredentials { ConsumerKey = "ConsumerKey", ConsumerSecret = "ConsumerSecret", OAuthToken = "TwitterAccessToken", AccessToken = "TwitterAccessTokenSecret" } }; using (var twitterCtx = new TwitterContext(auth, "https://api.twitter.com/1/", "https://search.twitter.com/")) { var tweet = twitterCtx.UpdateStatus("Hi everybody!"); // error here viewTextBlock.Text = String.Empty; viewTextBlock.Text = viewTextBlock.Text + "Status returned: " + "(" + tweet.StatusID + ")" + tweet.User.Name + ", " + tweet.Text + "\n"; } } 
+4
source share
2 answers
+1
source

You can implement it using the Twitterizer build. First, you can create a token that you can use to access Twitter, and then using this specific token, you can update TwitterStatus (Twitterizer.Core.TwitterObject.TwitterStatus). Sample code is as follows.

 public void CreateCachedAccessToken(string requestToken) { string ConsumerKey = ConfigurationManager.AppSettings["ConsumerKey"]; string ConsumerSecret = ConfigurationManager.AppSettings["ConsumerSecret"]; OAuthTokenResponse responseToken = OAuthUtility.GetAccessToken(ConsumerKey, ConsumerSecret, requestToken); //Cache the UserId Session["GetCachedUserId"] = responseToken.UserId; OAuthTokens accessToken = new OAuthTokens(); accessToken.AccessToken = responseToken.Token; accessToken.AccessTokenSecret = responseToken.TokenSecret; accessToken.ConsumerKey = ConsumerKey; accessToken.ConsumerSecret = ConsumerSecret; Session["AccessToken"] = accessToken; } 

To update TwitterStatus, you can do the following.

 public OAuthTokens GetCachedAccessToken() { if (Session["AccessToken"] != null) { return (OAuthTokens)(Session["AccessToken"]); } else { return null; } } TwitterStatus.Update(GetCachedAccessToken(), txtTweet.Trim()); 

The method below can be used to implement the sign.

  protected string GetTwitterAuthorizationUrl() { string ConsumerKey = ConfigurationManager.AppSettings["ConsumerKey"]; string ConsumerSecret = ConfigurationManager.AppSettings["ConsumerSecret"]; OAuthTokenResponse reqToken = OAuthUtility.GetRequestToken(ConsumerKey, ConsumerSecret); return "https://twitter.com/oauth/authorize?oauth_token=" + reqToken.Token; } 

Hope this helps. If there is any clarification, please raise. Thanks

0
source

All Articles