Question

I am writing an Metrol Style App to update status on my Twitter. I use LINQ to Twitter library. But I don't understand why my app throws exception 401 Unauthorized. 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";
    }
}
Was it helpful?

Solution

I just posted a blog entry on using OAuth in Windows 8 with LINQ to Twitter:

http://geekswithblogs.net/WinAZ/archive/2012/07/02/using-linq-to-twitter-oauth-with-windows-8.aspx

I also included a 401 FAQ in the LINQ to Twitter docs here:

http://linqtotwitter.codeplex.com/wikipage?title=LINQ%20to%20Twitter%20FAQ&referringTitle=Documentation

OTHER TIPS

You can implement it using the Twitterizer assembly. Firstly you can create a token which can be used to access Twitter and then using that particular 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 the TwitterStatus you can do as follows.

public OAuthTokens GetCachedAccessToken()
    {
        if (Session["AccessToken"] != null)
        {
            return (OAuthTokens)(Session["AccessToken"]);
        }
        else
        {
            return null;
        }
    }

TwitterStatus.Update(GetCachedAccessToken(), txtTweet.Trim());

The below mentioned method can be used to implement sign in.

   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 are any clarifications please raise. Thanks

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top