使用twitter api发布tweet时出错

本文关键字:出错 tweet 发布 twitter api 使用 | 更新日期: 2023-09-27 18:04:46

我尝试通过c#使用twitter api发布tweet。我有应用程序密钥和应用程序的权限为读和写。我也想获得访问令牌。但不能发推特或更新状态但是我每次都得到禁止错误异常。有人能帮我一下吗?

    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发布tweet时出错

我看到您有一个Authorization头,但是是一个简单的字符串。带有插入值的格式似乎不太可能有帮助。OAuth标准调用一个经过特殊处理的字符串,这里为Twitter描述:

https://dev.twitter.com/oauth/overview/creating-signatures

下面是我如何在LINQ中构建Twitter的授权字符串:

https://github.com/JoeMayo/LinqToTwitter/blob/master/src/LinqToTwitter/LinqToTwitter.Shared/Security/OAuth.cs

此外,OAuth过程充满了陷阱,因此我在FAQ中收集了一些可能会有所帮助的项目列表:

https://github.com/JoeMayo/LinqToTwitter/wiki/LINQ-to-Twitter-FAQ