AuthorizedClient.ScreenName returns null

Aug 16, 2012 at 9:54 AM
Edited Aug 16, 2012 at 1:50 PM

Hi, 

I'm working on a ASP.net Twitter Application.

I want show Current User's username on lblUser  but twitterCtx.AuthorizedClient.ScreenName returns null.

 var auth = new WebAuthorizer
            {
                Credentials = new SessionStateCredentials()
            };

            var twitterCtx = new TwitterContext(auth);

            try
            {
                lblUser.Text = twitterCtx.AuthorizedClient.ScreenName;
            }

i found another OAuth Api and i can do this with this code:

string url = "http://twitter.com/account/verify_credentials.xml";
string x = OAuth.oAuthWebRequest(oAuth.Method.GET, url, String.Empty);
XmlDocument xml = new XmlDocument();
            xml.LoadXml(x);
...
...
foreach (XmlNode elem in xml.DocumentElement.ChildNodes)
{
     if (elem.Name == "screen_name")
     {
         UserName = elem.InnerXml;
     }
     ...

How can i do this with linqtotwitter?

 
Coordinator
Aug 17, 2012 at 1:28 AM

Hi,

There are two ways to do this: via initial authentication session or with verify credentials.  The ScreenName in the Authorizer is only available when performing the full OAuth process.  However, you can get the current authenticated user's information, as you describe above with an Account/VerifyCredentials query.  Here's an example:

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

Notice that the User entity, from the Account entity, has an identifier property. That's where you get ScreenName and/or UserID. Here's the documentation that explains more:

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

Joe

 

Aug 17, 2012 at 11:56 AM

Thanks a lot Joe