Reputation: 1626
I'm doing some twitter experiment and finished all GET request from Twitter API reference, but for some reason I can't send a post request.
I'm trying to follow user on twitter pragmatically but for some reason I only got a forbidden error instead of JSON reponse. Am I doing something wrong on this?
Twitter API : POST friendship/create
The remote server returned an error: (403) Forbidden.
Here is my code;
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);
}
For authentication, I'm sure my credential is working because I used it on Twitter GET.
Upvotes: 0
Views: 51
Reputation: 2940
According to the API documentation, I suspect that you're already following the user hence the 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.
Upvotes: 1