BobbyDazzler
BobbyDazzler

Reputation: 1203

Linq2Twitter Single User Authorisation Null Value Exception

I've been developing an internal ASP.NET web forms application for our business and one of the requirements is to display our Twitter feed on our portal home page to all users.

For this I've decided that it is best to use LinqToTwitter Single User Authorisation to get the statuses for everyone without them having to authenticate their own accounts.

My main problem at the minute is that when we use the auth object to get the TwitterContext, it returns with an error on the TwitterContext saying

Value cannot be null

on every internal context object.

I've gone through our twitter application settings at http://dev.twitter.com and I have our correct consumer key/token and access key/token. The permission for the application is set to Read-Only. There is no callback URL specified on the http://dev.twitter.com website as it is currently on our internal system (so it wouldn't be able to get back anyway). Is this where it is going wrong? Do I need to forward some ports and allow the callback to get through to our development machines?

Here's the code for prudence. As far as I can see, there is nothing wrong with it. I know that it is set to .FirstOrDefault, this was just for seeing whether it is actually getting anything (which it isn't).

Thanks for any help you can give! :)

private async Task GetTweets()
{
    var auth = new SingleUserAuthorizer
    {
        CredentialStore = new SingleUserInMemoryCredentialStore
        {
            ConsumerKey = ConfigurationManager.AppSettings["consumerKey"],
            ConsumerSecret = ConfigurationManager.AppSettings["consumerSecret"],
            AccessToken = ConfigurationManager.AppSettings["accessToken"],
            AccessTokenSecret = ConfigurationManager.AppSettings["accessTokenSecret"],
        }
    };
    try
    {
        using (TwitterContext twitterContext = new TwitterContext(auth))
        {
            var searchResponse = await (from c in twitterContext.Status
                                        where c.Type == StatusType.User
                                        && c.ScreenName == "my_screenname"
                                        select c).ToListAsync();
            Tb_home_news.Text = searchResponse.FirstOrDefault().Text;
        }
    }
    catch (Exception ex)
    {
        Tb_home_news.Text = ex.Message;
    }
}

Upvotes: 2

Views: 257

Answers (1)

Joe Mayo
Joe Mayo

Reputation: 7513

If you're creating a Web app, you do need to add a URL to your Twitter App page. It isn't used for the callback, but might help avoid 401's in the future if you're using AspNetAuthorizer.

It looks like you have a NullReferenceException somewhere. What does ex.ToString() say?

Double check CredentialStore after initialization to make sure that all 4 credentials are populated. AccessToken and AccessTokenSecret come from your Twitter app page.

Does searchResponse contain any values? Calling FirstOrDefault on an empty collection will return null.

Upvotes: 1

Related Questions