Friendship/create method throws 401 error


#1

I am using following code to follow users. Please suggest what’s wrong here.

            var oauth_token = ConfigurationManager.AppSettings["TwitterAccessToken"];
            var oauth_token_secret = ConfigurationManager.AppSettings["TwitterconAccessTokenSecret"];
            var oauth_consumer_key = ConfigurationManager.AppSettings["TwitterconsumerKey"];
            var oauth_consumer_secret = ConfigurationManager.AppSettings["TwitterconsumerSecret"];

            // oauth implementation details
            var oauth_version = "1.0";
            var oauth_signature_method = "HMAC-SHA1";

            // unique request details
            var oauth_nonce = Convert.ToBase64String(
                new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
            var timeSpan = DateTime.UtcNow
                - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            var oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();

            var resource_url ="https://api.twitter.com/1.1/friendships/create.json"; //address of twitter API for follow user
            
            var screen_name = TwitterHandle;
            // create oauth signature
            var baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" +
                            "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&screen_name={6}";

            string TweetCount = Uri.EscapeDataString(System.Configuration.ConfigurationManager.AppSettings["TweetCount"].ToString());

            var baseString = string.Format(baseFormat,
                                        oauth_consumer_key,
                                        oauth_nonce,
                                        oauth_signature_method,
                                        oauth_timestamp,
                                        oauth_token,
                                        oauth_version
                                        ,
                                        Uri.EscapeDataString(TwitterHandle)
                                        );

            baseString = string.Concat("POST&", Uri.EscapeDataString(resource_url), "&", Uri.EscapeDataString(baseString));

            var compositeKey = string.Concat(Uri.EscapeDataString(oauth_consumer_secret),
                                    "&", Uri.EscapeDataString(oauth_token_secret));
            string oauth_signature;
            using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey)))
            {
                oauth_signature = Convert.ToBase64String(
                    hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString)));
            }

            // create the request header
            var headerFormat = "OAuth oauth_nonce=\"{0}\", oauth_signature_method=\"{1}\", " +
                               "oauth_timestamp=\"{2}\", oauth_consumer_key=\"{3}\", " +
                               "oauth_token=\"{4}\", oauth_signature=\"{5}\", " +
                               "oauth_version=\"{6}\"";

            var authHeader = string.Format(headerFormat,
                                    Uri.EscapeDataString(oauth_nonce),
                                    Uri.EscapeDataString(oauth_signature_method),
                                    Uri.EscapeDataString(oauth_timestamp),
                                    Uri.EscapeDataString(oauth_consumer_key),
                                    Uri.EscapeDataString(oauth_token),
                                    Uri.EscapeDataString(oauth_signature),
                                    Uri.EscapeDataString(oauth_version)
                            );
            // make the request
            ServicePointManager.Expect100Continue = false;

            var postBody = "?screen_name=bcci";//string.Format("?screen_name={0}", Uri.EscapeDataString(TwitterHandle));//"user_id=@sachin_rt&screen_name=sachin_rt";//            
            resource_url += postBody;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url);
            request.Headers.Add("Authorization", authHeader);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            
            WebResponse response = request.GetResponse();

Thanks in Advance


#2

I am using following api to follow user: https://api.twitter.com/1.1/friendships/create.json
I am also sending code in which I am getting 401 unauthorized error
https://api.twitter.com/1.1/friendships/create.json

var oauth_token = ConfigurationManager.AppSettings[“TwitterAccessToken”];
var oauth_token_secret = ConfigurationManager.AppSettings[“TwitterconAccessTokenSecret”];
var oauth_consumer_key = ConfigurationManager.AppSettings[“TwitterconsumerKey”];
var oauth_consumer_secret = ConfigurationManager.AppSettings[“TwitterconsumerSecret”];

            // oauth implementation details
            var oauth_version = "1.0";
            var oauth_signature_method = "HMAC-SHA1";

            // unique request details
            var oauth_nonce = Convert.ToBase64String(
                new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
            var timeSpan = DateTime.UtcNow
                - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            var oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();

            var resource_url = string.Empty;

          
                resource_url = "https://api.twitter.com/1.1/friendships/create.json"; //address of twitter API for follow user
           
            var screen_name = TwitterHandle;
            // create oauth signature
            var baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" +
                            "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&screen_name={6}";

            string TweetCount = Uri.EscapeDataString(System.Configuration.ConfigurationManager.AppSettings["TweetCount"].ToString());

            var baseString = string.Format(baseFormat,
                                        oauth_consumer_key,
                                        oauth_nonce,
                                        oauth_signature_method,
                                        oauth_timestamp,
                                        oauth_token,
                                        oauth_version
                                        ,
                                        Uri.EscapeDataString(TwitterHandle)
                                        );

            baseString = string.Concat("POST&", Uri.EscapeDataString(resource_url), "&", Uri.EscapeDataString(baseString));

            var compositeKey = string.Concat(Uri.EscapeDataString(oauth_consumer_secret),
                                    "&", Uri.EscapeDataString(oauth_token_secret));
            string oauth_signature;
            using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey)))
            {
                oauth_signature = Convert.ToBase64String(
                    hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString)));
            }

            // create the request header
            var headerFormat = "OAuth oauth_nonce=\"{0}\", oauth_signature_method=\"{1}\", " +
                               "oauth_timestamp=\"{2}\", oauth_consumer_key=\"{3}\", " +
                               "oauth_token=\"{4}\", oauth_signature=\"{5}\", " +
                               "oauth_version=\"{6}\"";

            var authHeader = string.Format(headerFormat,
                                    Uri.EscapeDataString(oauth_nonce),
                                    Uri.EscapeDataString(oauth_signature_method),
                                    Uri.EscapeDataString(oauth_timestamp),
                                    Uri.EscapeDataString(oauth_consumer_key),
                                    Uri.EscapeDataString(oauth_token),
                                    Uri.EscapeDataString(oauth_signature),
                                    Uri.EscapeDataString(oauth_version)
                            );
            // make the request
            ServicePointManager.Expect100Continue = false;

            var postBody = "?screen_name=bcci";//string.Format("?screen_name={0}", Uri.EscapeDataString(TwitterHandle));//"user_id=@abc&screen_name=abc";//            
            resource_url += postBody;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url);
            request.Headers.Add("Authorization", authHeader);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            
            WebResponse response = request.GetResponse();

Please suggest what I am doing wrong here or any other code to follow user.

Thanks in advance.
Vaibhav


#3

Same problem here!


#4

If this is still an issue (which for me it currently is) then to me this means that the API endpoint is no longer correctly functioning as i’ve rebuilt my own solution multiple times and it still kicks out either code 32 or a 401 Unauthorised error

Twitter Staff also seem to be ignoring that this is an issue.


#5

Seemingly this has been fixed - I left this for ages as it just wouldn’t work but its working fine now