How do I read a public twitter feed using .Net (on

2019-03-27 05:43发布

I'm trying to read the public twitter status of a user so I can display it in my Windows Phone application.

I'm using Scott Gu's example: http://weblogs.asp.net/scottgu/archive/2010/03/18/building-a-windows-phone-7-twitter-application-using-silverlight.aspx

When my code comes back from the async call, I get a "System.Security.SecurityException" as soon as I try to use the e.Result.

I know my uri is correct because I can plop it in the browser and get good results.

Here is my relavent code:

    public void LoadNewsLine()
    {
        WebClient twitter = new WebClient();

        twitter.DownloadStringCompleted += new DownloadStringCompletedEventHandler(twitter_DownloadStringCompleted);
        twitter.DownloadStringAsync(new Uri("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=krashlander"));          
    }

    void twitter_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        XElement xmlTweets = XElement.Parse(e.Result); //exception thrown here!

        var message = from tweet in xmlTweets.Descendants("status")
                      select tweet.Element("text").Value;

       //Set message and tell UI to update.
       //NewsLine = message.ToString(); 
       //RaisePropertyChanged("NewsLine");
    }

Any ideas anyone?

SOLUTION: I finally figured this one out. I had simply forgotten to uncomment the: capability in the WMAppManifest.xml. Once I did that the security exception went away.

6条回答
Evening l夕情丶
2楼-- · 2019-03-27 06:20

You should check out TweetSharp. It's working quite well for me. http://tweetsharp.codeplex.com

查看更多
Bombasti
3楼-- · 2019-03-27 06:27

You may want to have a look at the application provided here .This application show you tweets for a specified username and also allows you to export tweets to pdf,excel,word,image etc ..

http://blogs.gcpowertools.co.in/2012/05/activereporting-with-twitter.html

查看更多
Juvenile、少年°
4楼-- · 2019-03-27 06:29

I tried code you posted and no exception was thrown, getting results as expected, all seems to be just fine. So there must be something on your pc, net connection (proxy etc) which is causing this. What is detailed exception, StackTrace? Maybe Simple use of WebClient on Windows Phone 7 throws a NetworkError will help you?

查看更多
Bombasti
5楼-- · 2019-03-27 06:33

I hit something similar on my own app. This was my solution:

WebClient twitter=new WebClient();
twitter.OpenReadCompleted += new OpenReadCompletedEventHandler(twitter_OpenReadCompleted);
twitter.OpenReadAsync(new Uri("http://api.twitter.com..."));

and

void twitter_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) {
   ...
}
查看更多
劫难
6楼-- · 2019-03-27 06:40

This exception usually occurs when cross referencing threads in any way. In this case it is probably related to Webclient always returning on the UI thread. You can probably solve the problem by using HttpWebRequest instead (which is better performance wise anyway). Check this thread on msdn forums for more info

查看更多
女痞
7楼-- · 2019-03-27 06:42

I finally figured this one out. I had simply forgotten to uncomment the:

      <Capability Name="ID_CAP_NETWORKING"/>

capability in the WMAppManifest.xml. Once I did that the security exception went away.

查看更多
登录 后发表回答