【问题标题】:Error while getting public data from twitter从 Twitter 获取公共数据时出错
【发布时间】:2012-11-23 15:54:41
【问题描述】:
String status;
            IEnumerable<TwitterStatus> twitterStatus = twitterService.ListTweetsOnPublicTimeline();
            foreach(String status in twitterStatus)
            {
                Console.WriteLine(twitterStatus);
            }

为什么在foreach循环中出现can't convert string type error? 这是我的全部代码

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# 知识

【问题讨论】:

  • 错误说明了什么?你能把异常文本或消息放上去吗
  • 我更新它,请检查它,我需要输出,我不想要异常
  • 您还没有按照其他发帖人的要求发布异常详细信息。您的 twitterStatus 也是一个 IEnumerable。这个 TwitterStatus 类与字符串相同还是至少从字符串派生?
  • 我做到了,但在 for 循环中出现错误:对象引用未设置为对象的实例
  • @Amitd 我上传了我的整个代码

标签: c# twitter tweetsharp


【解决方案1】:

你会需要这样的东西..

 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

【讨论】:

  • 对象引用未设置为对象的实例。
  • 你需要按照这篇文章..一步一步的指南..你需要在 twiiter 上注册你的应用程序。 d80.co.uk/post/2011/02/13/…
  • 我已注册,因为我已经从 python 的 twitter 抓取数据集,现在正在使用 c#,我已注册
  • 更新了我的答案你能检查一下你得到的 response.StatusCode 吗?
  • 在 "if" 行设置一个断点并勾选 add a watch to response.StatusCode value and tweets list。
【解决方案2】:

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

你的问题很模糊,所以我假设你的 TitterStatus 对象有一个“文本”属性来回答这个问题。

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

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

【讨论】:

  • 使用new关键字创建对象实例!!!这是现在在 foreach 循环中显示的错误
  • 听起来 twitterStatus 为空。你的 TwitterService 调用是否返回值?
  • 谢谢!当您在服务调用 ListTweetsOnPublic...() 后中断时,“推文”的可枚举列表是否被填满?
  • 这让我很困惑,这就是我上传代码的原因,我认为可枚举的推文列表是空的
  • 您是否能够在 ListTweetsOnPublicTimeline() 调用后验证可枚举列表是否为空?如果是这样,您是否收到有关失败原因的任何错误信息?
猜你喜欢
  • 1970-01-01
  • 2015-04-18
  • 2022-11-17
  • 1970-01-01
  • 2012-02-25
  • 2019-06-30
  • 2012-05-27
相关资源
最近更新 更多