如何实用地关注 Twitter 上的用户
How to follow user on Twitter pragmatically
我正在做一些 Twitter 实验并完成了来自 Twitter API 参考的所有 GET 请求,但由于某些原因我无法发送 post 请求。
我试图在 Twitter 上务实地关注用户,但出于某种原因,我只收到禁止错误,而不是 JSON 响应。我在这方面做错了什么吗?
推特API:POST friendship/create
The remote server returned an error: (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 上使用了它。
根据 API documentation,我怀疑您已经在关注该用户,因此出现 403:
If the user is already friends with the user a HTTP 403 may be
returned, though for performance reasons this method may also return a
HTTP 200 OK message even if the follow relationship already exists.
我正在做一些 Twitter 实验并完成了来自 Twitter API 参考的所有 GET 请求,但由于某些原因我无法发送 post 请求。
我试图在 Twitter 上务实地关注用户,但出于某种原因,我只收到禁止错误,而不是 JSON 响应。我在这方面做错了什么吗?
推特API:POST friendship/create
The remote server returned an error: (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 上使用了它。
根据 API documentation,我怀疑您已经在关注该用户,因此出现 403:
If the user is already friends with the user a HTTP 403 may be returned, though for performance reasons this method may also return a HTTP 200 OK message even if the follow relationship already exists.