13

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?

Festys.rpo
  • 193
  • 1
  • 7

1 Answers1

22

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.

Joe Mayo
  • 7,501
  • 7
  • 41
  • 60
  • Oh, and I was looking only to OAout examples :( Thanks a lot! That works for me! – Festys.rpo May 06 '13 at 05:35
  • Joe, Auth.IsAuthorized always returns false even when it has called Authorize() and is successfully returning TwitterData. How can I reliably know when the app is authorized. I have found that I get uncaught exceptions in Linq2Twitter if I call Authorize() on every call that is made – DevDave May 13 '13 at 10:04
  • I just answered a similar question here: http://stackoverflow.com/questions/16520278/linqtotwitter-isauthorized-always-returns-false-for-applicationonlyauthorizer/16528192#16528192 – Joe Mayo May 13 '13 at 17:56