Arcadian
Arcadian

Reputation: 4350

twitter api in c# without pop authorize dialog

I want to write a console app that runs as a background task that searches twitter.

I want the job to kick off without popping up an authorize dialog. Is there a way I can call the twitter api without any dialog popping up?

I just found out that I need to use Application Only authentication. Where can I find a good example of application only authentication

Upvotes: 0

Views: 331

Answers (3)

fregate
fregate

Reputation: 11

Joe, works like a CHARM! Thanks a lot for you efforts to build such elegant solution on top of not so friendly REST interface. Still looking for different search criteria :)

Upvotes: 0

Joe Mayo
Joe Mayo

Reputation: 7513

You can use Single User Authorization or a newer featured called Application-Only authorization. Here's an example of how to do Application Only with LINQ to Twitter:

    private static void HandleApplicationOnlyAuthentication()
    {
        var auth = new ApplicationOnlyAuthorizer
        {
            Credentials = new InMemoryCredentials
            {
                ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"],
                ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"]
            }
        };

        auth.Authorize();
        //auth.Invalidate();

        var twitterCtx = new TwitterContext(auth);

        var srch =
            (from search in twitterCtx.Search
             where search.Type == SearchType.Search &&
                   search.Query == "LINQ to Twitter"
             select search)
            .SingleOrDefault();

        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));
    }

The call to Invalidate is commented out, but you'll want to put a timer on that and Twitter's rule of thumb is to invalidate the bearer token every 15 minutes or so.

The LINQ to Twitter site has documentation on Single User Authorization too. There's a Samples page on the site and the downloadable source code has working demos. BTW, Twitter user tokens generally don't ever expire, so you can store and continue to use the same tokens.

Upvotes: 2

DWRoelands
DWRoelands

Reputation: 4940

No, your console app cannot authorize without user interaction. The whole point of authorization is that users have to take action to allow applications to use their accounts.

Trust me, you really wouldn't want any application to be able to take control of your Twitter account without your explicit permission. :)

Upvotes: -1

Related Questions