Search code examples
twitterlinq-to-twitter

Linq to Twitter - Bad Authentication data


I've the the latest version of Linq to Twitter (3.1.2), and I'm receiving the "Bad Authentication data" error with the code below:

        var auth = new ApplicationOnlyAuthorizer
        {
            CredentialStore = new InMemoryCredentialStore
            {
                ConsumerKey = "xxxx",
                ConsumerSecret = "xxxx"
            }
        };

        using (var twitter = new TwitterContext(auth))
        {
            var users = twitter.User.Where(s => s.Type == UserType.Search && s.Query == "filter:verified").ToList();
        }

I thought at first that it could be Twitter taking a while to accept my new credentials, but I used Twitter's OAuth tool with my keys, and they produced tokens without issue. Any ideas what I'm missing here?

I could not find a duplicate, as the code referenced @ https://stackoverflow.com/questions/16387037/twitter-api-application-only-authentication-with-linq2twitter#= is no longer valid in the version I am running.


Solution

  • That query doesn't support Application-Only authorization. Here's the Twitter docs to that:

    https://dev.twitter.com/rest/reference/get/users/search

    Instead, you can use SingleUserAuthorizer, documented here:

    https://github.com/JoeMayo/LinqToTwitter/wiki/Single-User-Authorization

    Like this:

    var auth = new SingleUserAuthorizer
    {
        CredentialStore = new SingleUserInMemoryCredentialStore
        {
            ConsumerKey = ConfigurationManager.AppSettings["consumerKey"],
            ConsumerSecret = ConfigurationManager.AppSettings["consumerSecret"],
            AccessToken = ConfigurationManager.AppSettings["accessToken"],
            AccessTokenSecret = ConfigurationManager.AppSettings["accessTokenSecret"]
        }
    };
    

    To find out what type of authorization is possible, you can visit the L2T wiki at:

    https://github.com/JoeMayo/LinqToTwitter/wiki

    and each API query and command has a link at the bottom of the page to the corresponding Twitter API documentation.