Search code examples
c#twitter-oauthlinq-to-twitter

401 when attempting to Tweet with Linq to Twitter


So I've looked at all the of the suggestions from the Linq to Twitter documentation regarding 401 statuses with Oauth and I honestly don't know what I'm doing wrong.

var auth = new PinAuthorizer
        {
            Credentials = new InMemoryCredentials
            {
                ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"],
                ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"],
                //OAuthToken = ConfigurationManager.AppSettings["twitterOAuthToken"], //don't include this 
                //AccessToken = ConfigurationManager.AppSettings["twitterAccessToken"] //or this for new users. 
            },
            //
            UseCompression = true,
            GoToTwitterAuthorization = pageLink => Process.Start(pageLink),
            GetPin = () =>
                {
                    Console.WriteLine("/nAfter twitter authorizes your application you will be returned here or something/n");
                    Console.Write("Enter Pin here:");
                    return Console.ReadLine();
                }

        };

        auth.Authorize();


        using (var twitterCtx = new TwitterContext(auth, "https://api.twitter.com/1/",
            "https://search.twitter.com/"))
        {

            try
            {
                twitterCtx.Log = Console.Out;
                Console.WriteLine("Please provide tweet text");
                string tweet = Console.ReadLine();

                twitterCtx.UpdateStatus(tweet);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }


        }

I've ran this using the Pin Authentication method as well as the single user method (providing the oauth keys with config file). I'm able to query tweets but I can't update my status or send direct messages (I receive a 403 forbidden when I try to DM). I've provided a callback URL (albeit fake) so I can't think of why this isn't working. Any help would be appreciated.

PS this runs in Main, not sure if that matters


Solution

  • All you need is this overload of the TwitterContext ctor and it will use the proper base URLs:

    new TwitterContext(auth)
    

    The example you're using is for v1.0 URLs and LINQ to Twitter is on Twitter API v1.1 now. It will default to the proper base URLs.

    If you're querying okay, but getting errors on update and DM, double check to make sure you aren't trying to tweet the same text. That's why I append a DateTime.Now to the end of test tweets - to guarantee uniqueness.