Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法使用以下代码向Twitter发送更新。无误_C#_Twitter - Fatal编程技术网

C# 无法使用以下代码向Twitter发送更新。无误

C# 无法使用以下代码向Twitter发送更新。无误,c#,twitter,C#,Twitter,我尝试了以下代码: static void PostTweetanother(string username, string password, string tweet) { try { // encode the username/password string user = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(userna

我尝试了以下代码:

 static void PostTweetanother(string username, string password, string tweet)
    {
        try
        {
            // encode the username/password
            string user = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + password));
            // determine what we want to upload as a status
            byte[] bytes = System.Text.Encoding.ASCII.GetBytes("status=" + tweet);
            // connect with the update page
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://twitter.com/statuses/update.xml");
            // set the method to POST
            request.Method = "POST";
            request.ServicePoint.Expect100Continue = false; // thanks to argodev for this recent change!
            // set the authorisation levels
            request.Headers.Add("Authorization", "Basic " + user);
            request.ContentType = "application/x-www-form-urlencoded";
            // set the length of the content
            request.ContentLength = bytes.Length;

            // set up the stream
            Stream reqStream = request.GetRequestStream();
            // write to the stream
            reqStream.Write(bytes, 0, bytes.Length);
            // close the stream
            reqStream.Close();
        }
        catch (Exception ex) { throw ex; }
    }
我还尝试了以下代码:

 static void PostTweet(string username, string password, string tweet)
    {
        // Create a webclient with the twitter account credentials, which will be used to set the HTTP header for basic authentication
        WebClient client = new WebClient { Credentials = new NetworkCredential { UserName = username, Password = password } };

        // Don't wait to receive a 100 Continue HTTP response from the server before sending out the message body
        ServicePointManager.Expect100Continue = false;

        // Construct the message body
        byte[] messageBody = Encoding.ASCII.GetBytes("status=" + tweet);

        // Send the HTTP headers and message body (a.k.a. Post the data)
        client.UploadData("http://twitter.com/statuses/update.xml", messageBody);
    }

最近,我在互联网上看到twitter已经关闭了第三方客户端的RESTAPI,因此可能是因为这个原因,你不能发布更新。这是我的意见,所以我认为不可能做你想做的事


再见

这里有一些问题。首先,TwitterRESTAPI不再支持基本身份验证;你现在必须使用。第二,您引用了错误的端点;你应该打电话,就像这样
http://api.twitter.com/1/statuses/update.xml
(尽管我总是建议在与Twitter API交互时使用JSON而不是XML。)

由于您必须进行身份验证才能发布到Twitter,并且必须使用OAuth,因此我建议您使用类似于与Twitter API交互的Twitter库

如今,Twitter访问几乎成了一种商品,有很多很好的库(你不必维护)。你要做的是:发布状态更新,提取以下简化代码(示例使用Twitterizer):

为了获得OAuth凭据,您需要在您的帐户下注册您的应用程序。完成后,您可以在新创建的应用程序主页上找到您的用户密钥和密码,并且您的访问令牌和密码将位于页面右侧的我的访问令牌菜单项下链接的页面上

如果您只有一个用户,那么使用这一组凭据就可以了,代码示例所示的就是所谓的。但是,如果您希望允许其他人以自己的身份使用您的应用程序,那么您需要执行所谓的“OAuth舞蹈”,以便为您的每个用户获取访问令牌


有关完整OAuth流的更多信息,请访问,其中特别介绍了令牌交换。

我认为您应该检查您的服务器防火墙和身份验证。我已经在客户端服务器上进行了检查。REST API未关闭;Twitter最近停止了白名单,并敦促开发人员关注那些不是简单客户端的应用程序。问题的主要问题是代码使用的是基本身份验证。
//reference Twitterizer2.dll

var tokens = new Twitterizer.OAuthTokens {
    AccessToken = @"myAccessToken",
    AccessTokenSecret = @"myAccessTokenSecret",
    ConsumerKey = @"myConsumerKey",
    ConsumerSecret = @"myConsumerSecret"
};

// Post the update to twitter
var statusResponse = Twitterizer.TwitterStatus.Update(tokens, 
    "I am your status update!");
if (statusResponse.Result != Twitterizer.RequestResult.Success)
    return;

// Fetch the authenticated user's timeline, uses statuses/user_timeline
// under the hood
var timelineResponse = Twitterizer.TwitterTimeline.UserTimeline(tokens);
if (timelineResponse.Result != Twitterizer.RequestResult.Success)
    return;

foreach (var status in timelineResponse.ResponseObject)
{
    Console.WriteLine(status.Text);
}