How to use cached credentials

Apr 4, 2010 at 10:50 AM

Hi I am having some major problems with oauth. Can you please let me know if you can use cached credentials to get authorized. Or how can you signon with the credentials that are already cached.

Coordinator
Apr 4, 2010 at 9:13 PM

Hi Ronnie,

I'll explain in OAuth terminology.  The term "credentials" is often associated with username/password, but in OAuth you work with "tokens".  The "Request Token" is what you use to let the user authenticate with Twitter. Twitter will approve the Request Token when the user authenticates and then you use the Request Token to get an Access Token for that user.  It's the Access Token that you use to communicate with Twitter on that user's behalf.

Twitter does not expire a user's Access Token.  Therefore, you are free to cache the Access Token for subsequent requests.  Furthermore, you can save the Access Token in your database, associate it with the user, and use the same Access Token for all subsequent visits to your site.

For saving your Access Token, here's a post that could point you in the right direction:

http://linqtotwitter.codeplex.com/Thread/View.aspx?ThreadId=85528

You might want to peruse some of the other posts with questions on OAuth to see if they are helpful too.  If you get stuck, post a small sample of code that shows your problem and an explanation and I might be able to help you work through it.

Joe

Apr 5, 2010 at 12:13 AM

Ok the problem is I did all of the oauth stuff exchanged tokens saved pin and all. Have all credentials cached.  All I need is to know how to include the cached credentials to queries that needs authentification. Example code below. If you look into the try block where it says

 twitterCtx.AuthorizedClient.SignOn();
if (auth.IsAuthorized == true)
{

MessageBox.Show("Is Authorized");
}

I need to pass in the cachedcredentials or the access token that I already have to twitter. Because if I do it the way that I am now it is saying unauthorized. And the debugger is showing that it is coming from the
 twitterCtx.AuthorizedClient.SignOn(); part of the code.









 private void ultraButton11_Click(object sender, EventArgs e)
        {
            ITwitterAuthorization auth;

            if (string.IsNullOrEmpty(ConfigurationManager.AppSettings["twitterConsumerKey"]) || string.IsNullOrEmpty(ConfigurationManager.AppSettings["twitterConsumerSecret"]))
            {
                Console.WriteLine("Skipping OAuth authorization demo because twitterConsumerKey and/or twitterConsumerSecret are not set in your .config file.");
                Console.WriteLine("Using username/password authorization instead.");

                // For username/password authorization demo...
                auth = new UsernamePasswordAuthorization(Utilities.GetConsoleHWnd());
            }
            else
            {
               
                MessageBox.Show("Discovered Twitter OAuth consumer key in .config file.  Using OAuth authorization.");
                
                // For OAuth authorization demo...
                auth = new DesktopOAuthAuthorization();
                // If you wanted to pass the consumer key and secret in programmatically, you could do so as shown here.
                // Otherwise this information is pulled out of your .config file.
                //var desktopAuth = (DesktopOAuthAuthorization)auth;
                ////desktopAuth.ConsumerKey = "some key";
                ////desktopAuth.ConsumerSecret = "some secret";
            }

            auth.UseCompression = true;

            // TwitterContext is similar to DataContext (LINQ to SQL) or ObjectContext (LINQ to Entities)

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

                // For JTweeter (Laconica)
                //var twitterCtx = new TwitterContext(passwordAuth, "http://jtweeter.com/api/", "http://search.twitter.com/");

                // For Identi.ca (Laconica)
                //var twitterCtx = new TwitterContext(passwordAuth, "http://identi.ca/api/", "http://search.twitter.com/");

                // If we're using OAuth, we need to configure it with the ConsumerKey etc. from the user.
                if (twitterCtx.AuthorizedClient is OAuthAuthorization)
                {
                    InitializeOAuthConsumerStrings(twitterCtx);
                }

                // Whatever authorization module we selected... sign on now.  
                // See the bottom of the method for sign-off procedures.
                try
                {
                  
                    twitterCtx.AuthorizedClient.SignOn();
                    if (auth.IsAuthorized == true)
                    {
                        
                        MessageBox.Show("Is Authorized");
                    }
                }
                catch (Exception exe)
                {
                    MessageBox.Show(exe.Message);
                    return;
                }

                

                //
                // Sign-off, including optional clearing of cached credentials.
                //

                //auth.SignOff();
                //auth.ClearCachedCredentials();
            }

                       
        }

        private static void InitializeOAuthConsumerStrings(TwitterContext twitterCtx)
        {
            var oauth = (DesktopOAuthAuthorization)twitterCtx.AuthorizedClient;
            oauth.GetVerifier = () =>
            {
                Console.WriteLine("Next, you'll need to tell Twitter to authorize access.\nThis program will not have access to your credentials, which is the benefit of OAuth.\nOnce you log into Twitter and give this program permission,\n come back to this console.");
                Console.Write("Please enter the PIN that Twitter gives you after authorizing this client: ");
                return Console.ReadLine();
            };



            if (oauth.CachedCredentialsAvailable)
            {
                Console.WriteLine("Skipping OAuth authorization step because that has already been done.");
            }
        }
            
        }

Coordinator
Apr 5, 2010 at 3:49 PM

What version are you using - there was an OAuth bug in the 2.0.9 version and I released 2.0.9.1 with a fix. I just tested the sample code that you're running to make sure it doesn't have this bug and it works okay.  I just released v2.0.10, which you might want to download to ensure you aren't trying to work around the earlier bug.

Are you building a desktop or Web application?

Joe

Apr 5, 2010 at 4:41 PM

Hi I am building a desktop application.