This project has moved. For the latest updates, please go here.

AuthorizedClient.ScreenName returns null

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


I'm working on a 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);

                lblUser.Text = twitterCtx.AuthorizedClient.ScreenName;

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

string url = "";
string x = OAuth.oAuthWebRequest(oAuth.Method.GET, url, String.Empty);
XmlDocument xml = new XmlDocument();
foreach (XmlNode elem in xml.DocumentElement.ChildNodes)
     if (elem.Name == "screen_name")
         UserName = elem.InnerXml;

How can i do this with linqtotwitter?

Aug 17, 2012 at 1:28 AM


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;

                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:



Aug 17, 2012 at 11:56 AM

Thanks a lot Joe