从twitter获取公共数据时出错

时间:2012-11-23 15:54:41

标签: c# twitter tweetsharp

String status;
            IEnumerable<TwitterStatus> twitterStatus = twitterService.ListTweetsOnPublicTimeline();
            foreach(String status in twitterStatus)
            {
                Console.WriteLine(twitterStatus);
            }

为什么它在foreach循环中无法转换字符串类型错误? 这是我的全部代码

namespace TweetingTest
{
    class Program
    {
        static void Main(string[] args)
        {

            TwitterClientInfo twitterClientInfo = new TwitterClientInfo();
            twitterClientInfo.ConsumerKey = ConsumerKey; //Read ConsumerKey out of the app.config
            twitterClientInfo.ConsumerSecret = ConsumerSecret; //Read the ConsumerSecret out the app.config

            TwitterService twitterService = new TwitterService(twitterClientInfo);

            if (string.IsNullOrEmpty(AccessToken) || string.IsNullOrEmpty(AccessTokenSecret))
            {
                //Now we need the Token and TokenSecret

                //Firstly we need the RequestToken and the AuthorisationUrl
                OAuthRequestToken requestToken = twitterService.GetRequestToken();
                string authUrl = twitterService.GetAuthorizationUri(requestToken).ToString();

                //authUrl is just a URL we can open IE and paste it in if we want
                Console.WriteLine("Please Allow This App to send Tweets on your behalf");
                //Process.Start(authUrl); //Launches a browser that'll go to the AuthUrl.

                //Allow the App
                Console.WriteLine("Enter the PIN from the Browser:");
                string pin = Console.ReadLine();

                OAuthAccessToken accessToken = twitterService.GetAccessToken(requestToken, pin);

                string token = accessToken.Token; //Attach the Debugger and put a break point here
                string tokenSecret = accessToken.TokenSecret; //And another Breakpoint here

                Console.WriteLine("Write Down The AccessToken: " + token);
                Console.WriteLine("Write Down the AccessTokenSecret: " + tokenSecret);
            }

            twitterService.AuthenticateWith(AccessToken, AccessTokenSecret);

            //Console.WriteLine("Enter a Tweet");
            //string tweetMessage;
            //string data;
            //string ListTweetsOnPublicTimeline;
            //string TwitterUserStreamStatus = ListTweetsOnPublicTimeline();
            //TwitterStatus=ListTweetsOnPublicTimeline();
            //tweetMessage = Console.ReadLine();
            //ListTweetsOnPublicTimeline = Console.ReadLine();
            //TwitterStatus twitterStatus = twitterService.SendTweet(tweetMessage);
            //TwitterStatus twitterStatus = twitterService.ListTweetsOnPublicTimeline();
            //String status;
            IEnumerable<TwitterStatus> tweets = twitterService.ListTweetsOnPublicTimeline();
           foreach(var tweet in tweets)
            {
               Console.WriteLine(tweet);
                //Console.WriteLine("{0} says '{1}'", tweet.User.ScreenName, tweet.Text);
            }
            //twitterStatus=Console.ReadLine();
        }

这是我正在处理的整个代码,在foreach循环中只遇到一个错误,这是我在C#中缺乏知识

2 个答案:

答案 0 :(得分:0)

您正在通过的对象是“TwitterStatus”类型,而不是字符串...所以当您尝试将TwitterStatus对象自动转换为字符串时,您会感到困惑。

你的问题很模糊,所以我假设你的TitterStatus对象有一个“Text”属性用于这个答案。

foreach(TwitterStatus status in twitterStatus)
{
   Console.WriteLine(status.Text);
}
  

(只需将“.Text”替换为TwitterStatus的任何属性   持有状态文本)

答案 1 :(得分:0)

你需要这样的东西..

 using TweetSharp;

    TwitterService service = new TwitterService();
    IEnumerable<TwitterStatus> tweets = service.ListTweetsOnPublicTimeline();
    foreach (var tweet in tweets)
    {
        Console.WriteLine("{0} says '{1}'", tweet.User.ScreenName, tweet.Text);
    }

也尝试这段代码,看看它失败的原因..尝试调试response.StatusCode

using TweetSharp;

TwitterService service = new TwitterService();
IAsyncResult result = service.ListTweetsOnPublicTimeline(
    (tweets, response) =>
        {
            if(response.StatusCode == HttpStatusCode.OK)
            {
                foreach (var tweet in tweets)
                {
                    Console.WriteLine("{0} said '{1}'", tweet.User.ScreenName, tweet.Text);
                }
            }
        });

更多信息:https://github.com/danielcrenna/tweetsharp