Re-use session with PinAuthorizer

Apr 2, 2013 at 8:32 PM
Edited Apr 2, 2013 at 8:33 PM
Hi!

I've recently asked how to add a force_login parameter to prevent remembering credentials and force users to enter them every time.

Now I need to do a completely opposite thing - keep posting to Twitter even if user closes and reopens my desktop application, without requiring to use PinAuthorizer again.

Is it possible to somehow remember the authentication data and re-use it?

Thanks! Great job you've done on the component!
Coordinator
Apr 2, 2013 at 9:50 PM
Hi March,

Glad that's working for you. My choice for this type of thing would be to save credentials in a database and perform the operations via a Windows Service. Use the database because both your application and the service need access to the credentials and the service because it is running all the time. After you get credentials the first time, you can keep reusing them for that user.

You have different ways to communicate with the service. You could use a database table where the desktop app writes the jobs and the service pulls off the jobs and deletes them when done. Message queues work for this type of situation too, but if it's just a desktop app with no scalability requirements, that might be overkill.

I understand we're basically talking about app design here, so these are the first things that come to mind, but hopefully they might give you some ideas.

@JoeMayo
Apr 6, 2013 at 11:23 AM
Thanks for the info.

But I'm actually asking what session data should I save and re-use to avoid repeated pin authorization.

For example, I have the following code:
                var auth = new PinAuthorizer
                {
                    OAuthAuthorizeUrl = "https://api.twitter.com/oauth/authorize/",
                    Credentials = new InMemoryCredentials
                    {
                        ConsumerKey = "MyKey",
                        ConsumerSecret = "MySecret"
                    },
                    UseCompression = true,
                    GoToTwitterAuthorization = pageLink => authUrl = pageLink,
                    GetPin = f,
                };


                try
                {


                    auth.Authorize();



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

//Do some stuff here
                       
                    }

                    
                }
So after using PinAuthorizer, we're passing it to TwitterContext.

The question is how to persist successful authorization it to a more permanent storage, like application settings file, so it can be re-used again later.

Thanks!
Coordinator
Apr 6, 2013 at 6:28 PM
I see - after auth.Authorize(), all 4 credentials will be available. So, you can read these values from auth.Credentials properties: ConsumerKey, ConsumerSecret, OAuthToken, and AccessToken. Then save them for that user. The next time you want to do something on behalf of that user, load all 4 credentials. When the Authorizer has all 4 credentials, you don't need to call Authorize - just assign the authorizer to a new TwitterContext and use it.

There are other properties, ScreenName and UserID, and methods, Load and ToString, that you might find helpful.

@JoeMayo