Search code examples
c#asp.nettwitteroauthlinq-to-twitter

Twitter API application-only authentication (with linq2twitter)


I need to implement Twitter API application-only authentication and I've searched through linq2twitter oauth samples and stackoverflow questions, but I didn't find anything helpful about it.

Is it possible to implement this kind of authorization with linq2twitter and how?


Solution

  • Sure is. Here's an example:

            var auth = new ApplicationOnlyAuthorizer
            {
                CredentialStore = new InMemoryCredentialStore()
                {
                    ConsumerKey = "twitterConsumerKey",
                    ConsumerSecret = "twitterConsumerSecret"
                }
            };
    
            await auth.AuthorizeAsync();
    
            var twitterCtx = new TwitterContext(auth);
    
            var srch =
                await
                (from search in twitterCtx.Search
                 where search.Type == SearchType.Search &&
                       search.Query == "LINQ to Twitter"
                 select search)
                .SingleOrDefaultAsync();
    
            Console.WriteLine("\nQuery: {0}\n", srch.SearchMetaData.Query);
            srch.Statuses.ForEach(entry =>
                Console.WriteLine(
                    "ID: {0, -15}, Source: {1}\nContent: {2}\n",
                    entry.StatusID, entry.Source, entry.Text));
    

    There are running examples in the LinqToTwitterDemo project of the downloadable source code. The Program.cs file has an option for Application Only. There's also an OAuthDemos.cs file that has an example.