如何务实地在Twitter上关注用户

时间:2018-04-13 13:07:10

标签: c# twitter twitter-oauth

我正在做一些Twitter实验,并完成了Twitter API引用的所有GET请求,但出于某种原因,我无法发送帖子请求。

我试图在Twitter上实际关注用户,但出于某种原因我只得到一个禁止的错误而不是JSON响应。我在做错了吗?

Twitter API:POST friendship/create

  

远程服务器返回错误:(403)Forbidden。

这是我的代码;

try {
    var followFormat = "https://api.twitter.com/1.1/friendships/create.json?user_id={0}&follow=true";
    var followUrl = String.Format(followFormat, id);
    HttpWebRequest followRequest = (HttpWebRequest) WebRequest.Create(followUrl);
    var followHeaderFormat = "{0} {1}";
    followRequest.Headers.Add("Authorization", String.Format(followHeaderFormat, twitAuthResponse.token_type, twitAuthResponse.access_token));
    followRequest.Method = "POST";
    followRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
    followRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
    using(Stream stream = followRequest.GetRequestStream()) {
        byte[] content = Encoding.ASCII.GetBytes(postBody);
        stream.Write(content, 0, content.Length);
    }
    followRequest.Headers.Add("Accept-Encoding", "gzip");
    WebResponse followResponse = followRequest.GetResponse(); // Error Here
    User userResponse;
    using(followResponse) {
        using(var reader = new StreamReader(followResponse.GetResponseStream())) {
            JavaScriptSerializer js = new JavaScriptSerializer();
            var objectText = reader.ReadToEnd();
            userResponse = JsonConvert.DeserializeObject < User > (objectText);
        }
    }
    Console.WriteLine("Success: You followed {0}", userResponse.ScreenName);
} catch(Exception e) {
    Console.WriteLine("Failed To Follow: {0}", e.Message);
}

对于身份验证,我确定我的凭据有效,因为我在Twitter GET上使用它。

1 个答案:

答案 0 :(得分:0)

根据API documentation,我怀疑你已经关注了用户因此403:

  

如果用户已经是用户的朋友,则可能是HTTP 403   返回,但由于性能原因,此方法也可能返回a   即使以下关系已存在,也会显示HTTP 200 OK消息。