IsAuthorized is true as soon as the values are entered

Jul 13, 2012 at 5:33 PM

It appears that if you populate the credentials of a WebAuthorizer the IsAuthorized property is instantly set to true regardless to whether the values entered are valid.

Is there an easy way to test the credentials to ensure they are valid before attempting to tweet ?

Thanks,

Keri.

Coordinator
Jul 14, 2012 at 11:15 PM

Hi Keri,

You're correct in that IsAuthorized only checks to see if credentials are present, implicitly making the assumption that those credentials are valid.  To truly verify that the credentials are valid, you can use an Account/VerifyCredentials query, like this:

            var accounts =
                from acct in twitterCtx.Account
                where acct.Type == AccountType.VerifyCredentials
                select acct;

            try
            {
                Account account = accounts.SingleOrDefault();
                User user = account.User;
                Status tweet = user.Status ?? new Status();
                Console.WriteLine("User (#" + user.Identifier.ID
                                    + "): " + user.Identifier.ScreenName
                                    + "\nTweet: " + tweet.Text
                                    + "\nTweet ID: " + tweet.StatusID + "\n");

                Console.WriteLine("Account credentials are verified.");
            }
            catch (WebException wex)
            {
                Console.WriteLine("Twitter did not recognize the credentials. Response from Twitter: " + wex.Message);
            }

As you can see, if the credentials are not valid, Twitter returns an HTTP 401 Unauthorized, which results in a .NET WebException being thrown.  Here's the LINQ to Twitter docs on this:

http://linqtotwitter.codeplex.com/wikipage?title=Verifying%20Credentials&referringTitle=Managing%20Accounts

My design approach is to minimize unnecessary queries to Twitter, but in this case you have a point in that the semantics of IsAuthorized might logically assume that the query would be necessary.

Thanks for the feedback,

Joe

Jul 16, 2012 at 10:15 AM

Hi Joe,

Thanks for your reply,

I appreciate you want to minimise the hit on Twitter, I want to make sure the credentials are valid so i think for me the extra hit is justified, although I was going to call it on page load but after your comments I'll call it just before the tweet is sent :)

Keri.