0

I’m trying to get Linq to twitter to work on a wp8 App, i’ve looked throughout the available documentation and was not able to figure this out, when trying the demos (which the author says works for wp8) i get errors. Im using the ApplicationOnlyAuthorizer, my intention is just to be able to read the public tweets (not be able to log in and send tweets).

MainPage.cs

public MainPage()
{
     this.InitializeComponent();
     UserTweetsWidget = new UserTweetsViewModel("xxxxxxxx", 20);
     this.DataContext = this;
}

TweetModel.cs

public class TweetModel
{
    public string ScreenName { get; set; }
    public string UserName { get; set; }
    public string Image { get; set; }
    public string Text { get; set; }
    public string PublicationDate { get; set; }
}

UserTweetsViewModel.cs

public class UserTweetsViewModel
{
    public string Label { get; set; }
    public ObservableCollection<TweetModel> Tweets { get; set; }

    private const string consumerKey = “xxxxxxx”;
    private const string consumerSecret = “xxxxxx”;
    private const string twitterAccessToken = “xxxxxxxxxxxxxxxxx”;
    private const string twitterAccessTokenSecret = “xxxxxxxxxx”;

    public UserTweetsViewModel(string userName, int count)
    {
        String _n= userName;
        int _c= count;
        InitializeAsync(_n, _c);
    }

    private async Task InitializeAsync(string userName, int count)
    {
        this.Label = string.Format("Tweets by @{0}", userName);
        Tweets = await GetTwitterUserTimeLine(userName, count);
    }

    private async Task<ObservableCollection<TweetModel>> GetTwitterUserTimeLine(string userName, int count)
    {
        ObservableCollection<TweetModel> result = new ObservableCollection<TweetModel>();

        var auth = new ApplicationOnlyAuthorizer
        {
            CredentialStore = new InMemoryCredentialStore
            {
                ConsumerKey = consumerKey,
                ConsumerSecret = consumerSecret,
                OAuthToken= twitterAccessToken,
                OAuthTokenSecret= twitterAccessTokenSecret
            }
        };

        await auth.AuthorizeAsync();             
        TwitterContext twitterCtx = new TwitterContext(auth);

       var tweets = twitterCtx.Status.Where(tweet => tweet.ScreenName == userName && tweet.Type == StatusType.Home).Take(count).ToList();

        foreach (var item in tweets)
        {               
            TweetModel tweet = new TweetModel()
            {
                Text = item.Text,
                ScreenName = item.User.Name,
                UserName = "@" + item.ScreenName,
                PublicationDate = Convert.ToString(item.CreatedAt),
                Image = item.User.ProfileImageUrl                      
            };
            result.Add(tweet);
        }
        return result;
    }
}

Thanks, Bob

Bob Machine
  • 141
  • 4
  • 12

1 Answers1

2

The problem, i think, is you aren't awaiting the AuthorizeAsync method.

First, you need to convert your GetTwitterUserTimeLine into an async method:

private async Task<ObservableCollection<TweetModel>> GetTwitterUserTimeLine(string userName, int count)

Then, you can call auth.AuthorizeAsync, awaiting the call:

await auth.AuthorizeAsync();

This way, the method waits until the authorization is completed, before continue execution.

UPDATE:

To call this method from your viewmodel, You can create a new method in the ViewModel, called InitializeAsync:

private async Task InitializeAsync()

In that method, call the GetTwitterUserTimeLine this way:

Tweets = await GetTwitterUserTimeLine(username, count);

Finally, you need to call the InitializeAsync Method from your viewmodel constructor, and it's going to work.

Hope this helps.

Josue Yeray
  • 1,163
  • 1
  • 11
  • 12
  • i just tried that, but i get an error `Error 1 Cannot implicitly convert type 'System.Threading.Tasks.Task>' to 'System.Collections.ObjectModel.ObservableCollection'` – Bob Machine Feb 10 '14 at 14:25
  • You need to await the call to GetTwitterUserTimeLine also, to receive the ObservableCollection and not the task. – Josue Yeray Feb 10 '14 at 14:27
  • could u show me how to modify? This is how i have the call `public UserTweetsViewModel(string userName, int count) { this.Label = string.Format("Tweets by @{0}", userName); this.Tweets = GetTwitterUserTimeLine(userName, count); }` – Bob Machine Feb 10 '14 at 14:32
  • well the call is going but now i get stuck the next line, that should be the response data `var tweets = twitterCtx.Status.Where(tweet => tweet.ScreenName == userName && tweet.Type == StatusType.Home).Take(count).ToList();` on the constructor i have `public UserTweetsViewModel(string userName, int count) { String _n= userName; int _c= count; InitializeAsync(_n, _c); }` – Bob Machine Feb 10 '14 at 15:11
  • What is the problem you are having now? – Josue Yeray Feb 10 '14 at 15:20
  • well nothing happens, it's not showing the tweets, i will update my post and show u the 3 classes i have – Bob Machine Feb 10 '14 at 15:24
  • But, in the var tweets, after executing the query, are you getting any result? – Josue Yeray Feb 10 '14 at 15:28
  • no i'm not, that's where I'm stuck I have updated my post to show the 3 classes – Bob Machine Feb 10 '14 at 15:35
  • @BobMachine remove the ScreenName filter in the where clause. Home timeline only works on the authorized user. Also, you should use SingleUserAuthorizer because you're tweeting on behalf of a user. According to the documentation, ScreenName isn't an input parameter: http://linqtotwitter.codeplex.com/wikipage?title=Querying%20the%20Home%20Timeline&referringTitle=Making%20Status%20Queries%20and%20Calls – Joe Mayo Feb 10 '14 at 15:35
  • @BobMachine If you just want to search for a user's tweets, use a Search query and then you can use ApplicationOnlyAuthorizer: http://linqtotwitter.codeplex.com/wikipage?title=Searching%20Twitter&referringTitle=Performing%20Searches%20and%20Finding%20Trends – Joe Mayo Feb 10 '14 at 15:37
  • @JoeMayo I removed the ScreenName filter and still nothing. My intention is only to read the public timelines not to send tweets, my understanding was that ApplicationOnly was the mode for that, am i correct? – Bob Machine Feb 10 '14 at 15:40
  • @BobMachine You have to look at the API you're using. If the API operates on behalf of a user, then ApplicationOnly won't work. You should get a TwitterQueryException if you were using try/catch. Also, at the bottom of each documented API call is a link to the Twitter API documentation that tells you which type of authorization is valid. Twitter doesn't offer an API for a 3rd party to retrieve another person's home timeline. The only option to get someone's home timeline is for that user to authorize your app to do so. – Joe Mayo Feb 10 '14 at 15:53
  • @JoeMayo using ur suggestion I'm able to get a search result, for tweets that match that query, but what i was trying to do, was to get the public tweets, it was based on this example [link](http://code.msdn.microsoft.com/windowsapps/Twitter-widget-in-Windows-837bc147/view/SourceCode) but that is for w8 NOT wp8 that's what i was trying to do, i d/l the demos on codeplex but just got errors and was not able to use any as a starting point. So to get a user public tweets u say i need singleUserAuthorizer? Are there any working examples? – Bob Machine Feb 10 '14 at 16:05
  • Well, i think you need to take a deep look to the API. Now, your question about having a working example is complete... now your code works correctly – Josue Yeray Feb 10 '14 at 16:08
  • @JosueYeray ur 'kinda' right, I'm able to retrieve tweets but based on a search query not of a particular user, which is what i want to do. Hopefuly JoeMayo can shed some light on this. But thank you very much JosueYeray u have been very helpful, i just need a little extra push :) – Bob Machine Feb 10 '14 at 16:15
  • @BobMachine There are a couple differences between the link you provided and your code. The example was for the User timeline, but yours was for the Home timeline. That was an older version of LINQ to Twitter and the new version uses async. There's a Linq2TwitterDemos_WindowsPhone sample in the downloadable source code that shows proper syntax and the Linq2TwitterDemos_Console project has demos for every API call. – Joe Mayo Feb 10 '14 at 16:31
  • @BobMachine Thanks for providing that link. I added a comment to the sample to let everyone know that the latest version of LINQ to Twitter, v3.0, is async. – Joe Mayo Feb 10 '14 at 16:43
  • @JoeMayo I do stand corrected, I'm trying to get the User timeline. That's the thing i d/l the Linq2TwitterDemos_WindowsPhone but i can't build it it keeps saying references missing but i have set NuGet to enable restore, but still nothing – Bob Machine Feb 10 '14 at 16:49
  • @JoeMayo Im just trying to get the user timeline for a specific user at a time, i have tried all i can think of and i can't get it to work, like i said i can't get the demo to work i get reference error (i have cleaned/rebuild) and nothing. what i have is posted in my question, could you tell me what changes i need to make to get it to work, please. – Bob Machine Feb 10 '14 at 17:25
  • @BobMachine Read the name of the assembly and version where you have the error and add a reference. You could also create a new project and add LINQ to Twitter via NuGet and then use the code from the demo. The solution was built with VS 2013. I can look at LINQ to Twitter code and point you in the right direction, but I can't debug problems on your local machine. You need the basic knowledge of how to create a project in VS. – Joe Mayo Feb 10 '14 at 17:40
  • @JoeMayo I'm using VS Express 2012 for windows phone and it was causing some incompatibility but i have created a new solution and added the 2 projects and the demo is working now. I have been able to "Perform search"and use the SingleUserAuthorizer code, how do i now get the User timeline for a specific user? I'll just add it manually now but later add a textbox to get user input. – Bob Machine Feb 10 '14 at 18:19
  • @BobMachine The Twitter API (which LINQ to Twitter is based on) only offers the home timeline for whoever is the authorized user. If you aren't the authorized user, then you can fake it by manually building the home timeline yourself. Essentially, you have to manually do separate queries to get tweets (including retweets) for (1) the user, (2) all the user's followers, (3) and mentions of the user. I think that's everything that goes into a home timeline, but someone else might elaborate if I've forgotten. Then combine those lists and sort by date. – Joe Mayo Feb 10 '14 at 18:49
  • @JoeMayo what I'm trying to is exactly what is on [this example](http://code.msdn.microsoft.com/windowsapps/Twitter-widget-in-Windows-837bc147/view/SourceCode) but on windows phone 8, and this is done with linq to twitter too. I just cannot figure out what the differences are, that's what I'm asking ur help with? On that example I'm able to type a handle and get the public tweets for that handle and that's what I'm trying to do... – Bob Machine Feb 10 '14 at 19:08
  • Does anyone have any ideas on how to do this? – Bob Machine Feb 10 '14 at 21:48