【问题标题】:Error in Posting Tweets using twitter api使用 twitter api 发布推文时出错
【发布时间】: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。

标签: c# twitter


【解决方案1】:

我看到您有一个 Authorization 标头,但是带有插入值的简单 string.Format 似乎不太可能有帮助。 OAuth 标准要求专门处理的字符串,此处针对 Twitter 进行了描述:

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

这是我在 LINQ to Twitter 中构建该授权字符串的方法:

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

此外,OAuth 流程在此过程中充满了陷阱,因此我收集了一份可能有助于常见问题解答的项目列表:

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

【讨论】:

    猜你喜欢
    • 2012-03-28
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 2013-04-03
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    相关资源
    最近更新 更多