[英]Getting error : The remote server returned an error: (401) Unauthorized. , while posting my website comments to twitter. Using C#
Am trying to post my website comments on my twitter wall, but i am getting an error 我正在尝试在Twitter墙上张贴我的网站评论,但出现错误
The remote server returned an error: (401) Unauthorized
远程服务器返回错误:(401)未经授权
My tried code is as :- 我试过的代码是:-
using System;
using System.Web;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;
using System.Text;
using System.Configuration;
namespace Dotned.UI.Framework
{
public class TwitterClient
{
public string Username { get; set; }
public string Password { get; set; }
public Exception Error { get; set; }
private string _twitterUpdateUrl = "http://twitter.com/statuses/update.json";
public TwitterClient(string userName, string password)
{
this.Username = userName;
this.Password = password;
}
public void SendMessage(string message)
{
try
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(_twitterUpdateUrl);
request.Credentials = new NetworkCredential(this.Username, this.Password);
SetRequestParams(request);
string post = string.Format("status={0}", HttpUtility.UrlEncode(message));
using (Stream requestStream = request.GetRequestStream())
{
using (StreamWriter writer = new StreamWriter(requestStream))
{
writer.Write(post);
}
}
WebResponse response = request.GetResponse();
string content;
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(responseStream))
{
content = reader.ReadToEnd();
}
}
}
catch (Exception ex)
{
Error = ex;
}
}
private static void SetRequestParams(HttpWebRequest request)
{
System.Net.ServicePointManager.Expect100Continue = false;
request.Timeout = 50000;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
}
}
}
The process is getting break at WebResponse response = request.GetResponse();
WebResponse response = request.GetResponse();
的过程正在中断WebResponse response = request.GetResponse();
. 。
Any suggestions are highly appreciable. 任何建议都是非常可取的。
You must authenticate with OAuth. 您必须通过OAuth进行身份验证。 You can either visit Twitter's OAuth Page to see how to write this yourself or visit their 3rd Party Library Page to find a library to help you.
您可以访问Twitter的OAuth页面以了解如何编写该信息,也可以访问其第三方图书馆页面以找到可以帮助您的图书馆。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.