convert WP7IsolatedStorageCredentialsDemo to linqtotwitter 2.1

Jan 28, 2013 at 3:10 PM
Edited Jan 28, 2013 at 4:53 PM

Hi Guys,

I'm going crazy, I made everything work with 2.0.2 library, but now I need to update to 2.1.3 and I can't figure out how to makw it works... I lost all day !!! Can someone help me please ???

here is my code, file oauth.xaml:

 void Page_Loaded(object sender, RoutedEventArgs e)
        {
            pinAuth = new PinAuthorizer
            {
                Credentials = new IsolatedStorageCredentials 
                {
                    ConsumerKey = "xxx",
                    ConsumerSecret = "xxx" 
                },
                UseCompression = true,
                GoToTwitterAuthorization = pageLink => Dispatcher.BeginInvoke(() => OAuthWebBrowser.Navigate(new Uri(pageLink, UriKind.Absolute)))
            };

            pinAuth.BeginAuthorize(
                resp => Dispatcher.BeginInvoke(() => 
                {
                    switch (resp.Status)
                    {
                        case TwitterErrorStatus.Success:
                            break;
                        case TwitterErrorStatus.TwitterApiError:
                        case TwitterErrorStatus.RequestProcessingException:
                            MessageBox.Show(
                                resp.Error.ToString(),
                                resp.Message,
                                MessageBoxButton.OK);
                            break;
                    }
                }));
        }

        private void AuthenticateButton_Click(object sender, RoutedEventArgs e)
        {
            pinAuth.CompleteAuthorize(
                PinTextBox.Text,
                completeResp => Dispatcher.BeginInvoke(() =>
                {
                    switch (completeResp.Status)
                    {
                        case TwitterErrorStatus.Success:
                            var cred = pinAuth.Credentials as IsolatedStorageCredentials;
                            cred.Save();
                            NavigationService.GoBack();
                            break;
                        case TwitterErrorStatus.TwitterApiError:
                        case TwitterErrorStatus.RequestProcessingException:
                            MessageBox.Show(
                                completeResp.Error.ToString(),
                                completeResp.Message,
                                MessageBoxButton.OK);
                            break;
                    }
                }));
        }

file mainpage.xaml:

     private void LoadProfile()
        {        
            var ctx = new TwitterContext();

            (from tweet in ctx.Status
                where tweet.Type == StatusType.User && tweet.ScreenName == "amchamlat"
                select tweet)
            .AsyncCallback(tweets =>
                Dispatcher.BeginInvoke(() =>
                {
                    var publicTweets = (from tweet in tweets
                                            select tweet).FirstOrDefault();

                    System.Diagnostics.Debug.WriteLine(publicTweets.User.FollowersCount.ToString()); //total followers
                    System.Diagnostics.Debug.WriteLine(publicTweets.User.FriendsCount.ToString()); //total following
                    System.Diagnostics.Debug.WriteLine(publicTweets.User.Description.ToString()); //description
                    System.Diagnostics.Debug.WriteLine(publicTweets.User.Name.ToString()); //name
                    System.Diagnostics.Debug.WriteLine(publicTweets.User.ProfileImageUrl.ToString()); //image
                    System.Diagnostics.Debug.WriteLine(publicTweets.User.StatusesCount.ToString()); //total tweets
                }))
            .SingleOrDefault();            
        }

 

Coordinator
Jan 28, 2013 at 3:22 PM

Hi,

First thing - you accidentally posted your credentials. Maybe edit your post to remove them - but it would be better to visit your Twitter App page and re-generate keys ASAP.

In Twitter API v1.1, which LINQ to Twitter supports in v2.1.x.x and above, all queries must be authenticated and it looks like you're trying to do that. It looks like the only thing you need to do is pass the authorizer to your TwitterContext instance:

var ctx = new TwitterContext(pinAuth);

@JoeMayo

 

Jan 29, 2013 at 7:43 AM
Edited Jan 29, 2013 at 7:44 AM

perfect joe thank you so much I made this way and works.

I made many different queries, but for this one is there a easy way to get profile info without webbrowser authorization? (there are all public infos)

thanks

private void LoadProfile()
        {
            ITwitterAuthorizer pinAuth =
                new PinAuthorizer
                {
                    Credentials = new IsolatedStorageCredentials()

                };

            if (pinAuth == null || !pinAuth.IsAuthorized)
            {
                NavigationService.Navigate(new Uri("/OAuth.xaml", UriKind.Relative));
            }
            else
            {

                var ctx = new TwitterContext(pinAuth);

                (from tweet in ctx.Status
                 where tweet.Type == StatusType.User && tweet.ScreenName == "amchamlat"
                 select tweet)
                .AsyncCallback(tweets =>
                    Dispatcher.BeginInvoke(() =>
                    {
                        var publicTweets = (from tweet in tweets
                                            select tweet).FirstOrDefault();

                        System.Diagnostics.Debug.WriteLine(publicTweets.User.FollowersCount.ToString()); //total followers
                        System.Diagnostics.Debug.WriteLine(publicTweets.User.FriendsCount.ToString()); //total following
                        System.Diagnostics.Debug.WriteLine(publicTweets.User.Description.ToString()); //description
                        System.Diagnostics.Debug.WriteLine(publicTweets.User.Name.ToString()); //name
                        System.Diagnostics.Debug.WriteLine(publicTweets.User.ProfileImageUrl.ToString()); //image
                        System.Diagnostics.Debug.WriteLine(publicTweets.User.StatusesCount.ToString()); //total tweets
                    }))
                .SingleOrDefault();
            }
        }
Jan 29, 2013 at 8:34 AM
Edited Jan 29, 2013 at 8:52 AM

I solved using this type of auth :)

But I have last question... How can I simplify the query, with only one??? thanks

private void LoadProfile()
        {
            var auth = new SingleUserAuthorizer
            {
                Credentials = new InMemoryCredentials
                {
                    ConsumerKey = "xxx",
                    ConsumerSecret = "xxx",
                    AccessToken = "xxx-xxx",
                    OAuthToken = " xxx"
                }
            };

            using (var twitterCtx = new TwitterContext(auth, "https://api.twitter.com/1/", "https://search.twitter.com/"))
            {
                //Log
                twitterCtx.Log = Console.Out;

                var queryResponse = (from tweet in twitterCtx.Status
                             where tweet.Type == StatusType.User && tweet.ScreenName == "amchamlat"
                             select tweet);

                queryResponse.AsyncCallback(tweets =>
                   Dispatcher.BeginInvoke(() =>
                   {
                       var publicTweets = (from tweet in tweets
                                           select tweet).FirstOrDefault();

                       System.Diagnostics.Debug.WriteLine(publicTweets.User.FollowersCount.ToString()); //total followers
                       System.Diagnostics.Debug.WriteLine(publicTweets.User.FriendsCount.ToString()); //total following
                       System.Diagnostics.Debug.WriteLine(publicTweets.User.Description.ToString()); //description
                       System.Diagnostics.Debug.WriteLine(publicTweets.User.Name.ToString()); //name
                       System.Diagnostics.Debug.WriteLine(publicTweets.User.ProfileImageUrl.ToString()); //image
                       System.Diagnostics.Debug.WriteLine(publicTweets.User.StatusesCount.ToString()); //total tweets
                   }))
               .SingleOrDefault();
            }
        }
Coordinator
Jan 29, 2013 at 1:00 PM

You should instantiate your TwitterContext like this:

var twitterCtx = new TwitterContext(auth)

Those URLs were for the previous version of the Twitter API and LINQ to Twitter is not using Twitter API v1.1.

You can do one query like this:

(from tweet in twitterCtx.Status
 where tweet.Type == StatusType.User && 
           tweet.ScreenName == "amchamlat"
 select tweet)
.AsyncCallback(tweets =>
                   Dispatcher.BeginInvoke(() =>
                   {
                       var publicTweets = (from tweet in tweets
                                           select tweet).FirstOrDefault();

                       System.Diagnostics.Debug.WriteLine(publicTweets.User.FollowersCount.ToString()); //total followers
                       System.Diagnostics.Debug.WriteLine(publicTweets.User.FriendsCount.ToString()); //total following
                       System.Diagnostics.Debug.WriteLine(publicTweets.User.Description.ToString()); //description
                       System.Diagnostics.Debug.WriteLine(publicTweets.User.Name.ToString()); //name
                       System.Diagnostics.Debug.WriteLine(publicTweets.User.ProfileImageUrl.ToString()); //image
                       System.Diagnostics.Debug.WriteLine(publicTweets.User.StatusesCount.ToString()); //total tweets
                   }))
               .SingleOrDefault();

There's also a newer MaterializedAsyncCallback that lets you handle exceptions better:

            (from tweet in twitterCtx.Status
             where tweet.Type == StatusType.Home
             select tweet)
            .MaterializedAsyncCallback(resp =>
             {
                 if (resp.Status != TwitterErrorStatus.Success)
                     throw resp.Exception;

                 Console.WriteLine("\nTweets for " + twitterCtx.UserName + "\n");
                 foreach (var tweet in resp.State)
                 {
                     Console.WriteLine(
                         "Friend: " + tweet.User.Identifier.ScreenName +
                         "\nRetweeted by: " +
                             (tweet.Retweeted ?
                                tweet.RetweetedStatus.User.Name :
                                "Original Tweet") +
                         "\nTweet: " + tweet.Text + "\n");
                 }
             });

@JoeMayo

 

Jan 29, 2013 at 3:02 PM
Edited Jan 29, 2013 at 3:02 PM

thank you so much for your advices Joe, yes I used this for tweet, retweet and reply because I need that user login and authorize app

var twitterCtx = new TwitterContext(auth)

But when the app starts and the user starts to authorize the app and tweet I should get this datas from a username : total followers, total following, description, name, image, total tweets, I'd like also to use just one query it's possible to call just once, without get user infos form tweets but just from the screename?

(from tweet in twitterCtx.Status
 where tweet.Type == StatusType.User && 
           tweet.ScreenName == "amchamlat"
 select tweet)
.AsyncCallback(tweets =>
                   Dispatcher.BeginInvoke(() =>
                   {
                       var publicTweets = (from tweet in tweets
                                           select tweet).FirstOrDefault();

I tried this but didn't work:
http://linqtotwitter.codeplex.com/wikipage?title=Querying%20User%20Info&referringTitle=Getting%20User%20Information
http://linqtotwitter.codeplex.com/wikipage?title=Querying%20User%20Details&referringTitle=Getting%20User%20Information
http://linqtotwitter.codeplex.com/wikipage?title=Searching%20for%20Users&referringTitle=Getting%20User%20Information

ALWAYS THANKS