简体   繁体   English

从 Twitter 获取公共数据时出错

[英]Error while getting public data from twitter

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

Why it give can not convert string type error in foreach loop ?为什么它在 foreach 循环中给出无法转换字符串类型的错误? this is my whole code这是我的全部代码

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();
        }

This is my whole code on which I am working and facing just one error on foreach loop which is my lack of knowledge in C#这是我正在处理的整个代码,并且在 foreach 循环中只面临一个错误,这是我缺乏 C# 知识

The object you are itterating through is of type "TwitterStatus", not string... so you are confusing it when you try to automatically cast a TwitterStatus object as a string.您正在遍历的对象是“TwitterStatus”类型,而不是字符串……所以当您尝试将 TwitterStatus 对象自动转换为字符串时,您会混淆它。

Your question is very vague, so I'm going to assume your TitterStatus object has a "Text" property for the purposes of this answer.您的问题非常模糊,因此出于此答案的目的,我将假设您的 TitterStatus 对象具有“Text”属性。

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

(just replace the ".Text" with whatever property of TwitterStatus holds the status text) (只需将“.Text”替换为 TwitterStatus 包含状态文本的任何属性)

You will need something like this..你会需要这样的东西..

 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);
    }

Also try this code to see why it is failing.. try to debug response.StatusCode也试试这个代码,看看它为什么失败.. 尝试调试 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);
                }
            }
        });

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM