【发布时间】:2015-10-16 11:53:36
【问题描述】:
我尝试通过 c# 使用 twitter api 发布推文。 我有应用程序密钥和appscecrt,应用程序权限为读写。 我也很想获得访问令牌。 但不能发布推文或更新状态 但是我每次都得到禁止的错误异常。谁能帮我这个。
public class TwitAuthenticateResponse
{
public string token_type { get; set; }
public string access_token { get; set; }
}
class twitterSupporter{
public void postTweets()
{
try
{
// api for posting tweets
var timelineFormat = "https://api.twitter.com/1.1/statuses/update.json";
HttpWebRequest timeLineRequest = (HttpWebRequest)WebRequest.Create(timelineFormat);
var timelineHeaderFormat = "{0} {1}";
timeLineRequest.Headers.Add("Authorization", string.Format(timelineHeaderFormat, twitAuthResponse.token_type, twitAuthResponse.access_token));
timeLineRequest.Method = "Post";
var timeLineJson = string.Empty;
var status = "its done";
using (Stream stream = timeLineRequest.GetRequestStream())
{
byte[] content = ASCIIEncoding.ASCII.GetBytes(status);
stream.Write(content, 0, content.Length);
}
WebResponse timeLineResponse = timeLineRequest.GetResponse();
using (timeLineResponse)
{
using (var reader = new StreamReader(timeLineResponse.GetResponseStream()))
{
timeLineJson = reader.ReadToEnd();
}
}
}
catch (WebException we)
{
//handle web exception
}
}
【问题讨论】:
-
您收到禁止响应的唯一原因是您的凭据之一不正确。查看所有详细信息并确保您也是 twitter 的注册用户,因为您必须是注册且知名的客户才能使用 api。