Get all followings

Feb 6, 2013 at 11:20 AM
Hi,
I've just started a Windows Phone app, and I need to get all the user's followings. I tried this :
SharedState.Authorizer = pinAuth;                        
ITwitterAuthorizer auth = SharedState.Authorizer;
TwitterContext twitterCtx = new TwitterContext(auth);

    var friendList =
        (from friend in twitterCtx.SocialGraph
            where friend.Type == SocialGraphType.Friends && friend.ScreenName == "_TDK"
            select friend)
        .SingleOrDefault();

    List<String> Followings;
    foreach (var id in friendList.ScreenName)
    {
        Followings.Add(id.ToString());
    }
But friendlist is always null and, obviously, the foreach does not like that and throws an exception.

Could someone help me ?

Thanks.

(You can answer on StackOverflow too. )
Coordinator
Feb 6, 2013 at 6:18 PM
Edited Feb 6, 2013 at 6:19 PM
Hi TDK,

I think you need to iterate over the IDs collection, like this:
    foreach (var id in friendList.IDs)
    {
        Followings.Add(id.ToString());
    }
Joe
Feb 7, 2013 at 10:17 AM
Edited Feb 7, 2013 at 10:18 AM
FriendList is null, it does not change anything, the problem is here I think, but I can't find what's the matter... Maybe the twittercontext ?
(from friend in twitterCtx.SocialGraph
            where friend.Type == SocialGraphType.Friends && friend.ScreenName == "_TDK"
            select friend)
        .SingleOrDefault();
Coordinator
Feb 7, 2013 at 6:47 PM
Do the demos in either the downloadable source or ConsoleDemos work for you? Your code looks very similar to the demos, so I'm wondering if it might be related to the authorizer too. Maybe run Fiddler to see what Twitter is returning - which should be a JSON object with an array of IDs.

Joe
Feb 8, 2013 at 11:53 AM
Edited Feb 8, 2013 at 11:55 AM
I tried to paste my code in "WindowsPhoneDemo" to be sure, and it does not work too :/
I don't know how to use Fiddler..

But I can post an update from my app, so I think the authorizer should works, isn't it ?
Coordinator
Feb 9, 2013 at 10:40 PM
My apologies for not seeing this sooner, but you need to make async calls with Silverlight-based apps, including Windows Phone. Here's an example of how you can refactor the query:
                var twitterCtx = new TwitterContext(auth);

                (from social in twitterCtx.SocialGraph
                 where social.Type == SocialGraphType.Followers &&
                       social.ScreenName == "JoeMayo"
                 select social)
                .MaterializedAsyncCallback(asyncResponse =>
                    Dispatcher.BeginInvoke(() =>
                    {
                        if (asyncResponse.Status != TwitterErrorStatus.Success)
                        {
                            MessageBox.Show("Error during query: " + asyncResponse.Exception.Message);
                            return;
                        }

                        SocialGraph social = asyncResponse.State.SingleOrDefault();

                        SocialListBox.ItemsSource = social.IDs;
                    }));
The MaterializedAsyncCallback manages the callback from Twitter. Notice how I use Dispatcher.BeginInvoke to marshal the call back onto the UI thread as the callback is on a worker thread. On the asyncResponse callback parameter, use Status to see if there is an error and use State to get the data if the query is successful.

@JoeMayo
Feb 9, 2013 at 11:25 PM
Ok, may I come to hug you ?
It works perfectly ! :D
Great thanks ! :D
Feb 10, 2013 at 1:31 AM
Edited Feb 10, 2013 at 1:31 AM
Another question,
How to get the @username of the usuers from the IDs ?
List<String> Followings;
foreach (String follwing in social.IDs)
                        {
                            (from user in twitterCtx.User
                             where user.Type == UserType.Lookup &&
                             user.UserID == follwing 
                             select user)
                             .MaterializedAsyncCallback(asyncResponse2 =>
                                 Dispatcher.BeginInvoke(() =>
                                 {
                                     if (asyncResponse2.Status != TwitterErrorStatus.Success)
                                     {
                                         MessageBox.Show("Error during query: " + asyncResponse2.Exception.Message);
                                         return;
                                     }
                                     User user = asyncResponse2.State.SingleOrDefault();
                                     Followings.Add(user.Identifier.ScreenName);
                                 }));
}
But this, does not work. It does not return anything, it seems like the code does not execute the function unless if an exception is thrown.
Coordinator
Feb 10, 2013 at 9:07 PM
I'm not sure what is happening there because I can't reproduce. I did add a couple new WP8 Demo projects to the Samples Page, where one of the demos is for Lookup:

BTW, the Lookup API takes a comma-separated list of up to 100 users. This can improve performance and use less of your allocated rate limit.

@JoeMayo
Feb 11, 2013 at 5:04 PM
It's works ! Thanks again :D