The remote server returned an error: (403) Forbidden


#1
  public string UploadMedia(string status, string imageFile)
        {
            string oauth_consumer_key = oauthconsumerkey;
            string oauth_consumerSecret = oauthconsumersecret;
            string oauth_signature_method = "HMAC-SHA1";
            string oauth_version = "1.0";
            string oauth_token = oauthAccessToken;
            string oauth_token_secret = oauthAccessTokenSecret;
            string oauth_nonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
            TimeSpan timeSpan = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            string oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();
            SortedDictionary<string, string> basestringParameters = new SortedDictionary<string, string>();
            basestringParameters.Add("oauth_version", oauth_version);
            basestringParameters.Add("oauth_consumer_key", oauth_consumer_key);
            basestringParameters.Add("oauth_nonce", oauth_nonce);
            basestringParameters.Add("oauth_signature_method", oauth_signature_method);
            basestringParameters.Add("oauth_timestamp", oauth_timestamp);
            basestringParameters.Add("oauth_token", oauth_token);

            string baseString = String.Empty;

            baseString += "POST" + "&";

            baseString += Uri.EscapeDataString("https://api.twitter.com/1.1/statuses/update_with_media.json") + "&";

            foreach (KeyValuePair<string, string> entry in basestringParameters)
            {
                baseString += Uri.EscapeDataString(entry.Key + "=" + entry.Value + "&");
            }

            baseString = baseString.Substring(0, baseString.Length - 3);

            string signingKey = Uri.EscapeDataString(oauth_consumerSecret) + "&" + Uri.EscapeDataString(oauth_token_secret);

            HMACSHA1 hasher = new HMACSHA1(new ASCIIEncoding().GetBytes(signingKey));

            string signatureString = Convert.ToBase64String(hasher.ComputeHash(new ASCIIEncoding().GetBytes(baseString)));
            //ServicePointManager.UseNagleAlgorithm = true;
            ServicePointManager.Expect100Continue = false;


            HttpWebRequest tweetRequest = (HttpWebRequest)WebRequest.Create("https://api.twitter.com/1.1/statuses/update_with_media.json");
            tweetRequest.KeepAlive = false;
            tweetRequest.ProtocolVersion = HttpVersion.Version10;
            tweetRequest.Method = "POST";

            tweetRequest.PreAuthenticate = true;

            tweetRequest.AllowWriteStreamBuffering = true;

            string authorizationHeaderParams = String.Empty;

            authorizationHeaderParams += "OAuth ";

            authorizationHeaderParams += "oauth_nonce=" + "\"" + Uri.EscapeDataString(oauth_nonce) + "\",";

            authorizationHeaderParams += "oauth_signature_method=" + "\"" + Uri.EscapeDataString(oauth_signature_method) + "\",";

            authorizationHeaderParams += "oauth_timestamp=" + "\"" + Uri.EscapeDataString(oauth_timestamp) + "\",";

            authorizationHeaderParams += "oauth_consumer_key=" + "\"" + Uri.EscapeDataString(oauth_consumer_key) + "\",";

            authorizationHeaderParams += "oauth_token=" + "\"" + Uri.EscapeDataString(oauth_token) + "\",";

            authorizationHeaderParams += "oauth_signature=" + "\"" + Uri.EscapeDataString(signatureString) + "\",";

            authorizationHeaderParams += "oauth_version=" + "\"" + Uri.EscapeDataString(oauth_version) + "\"";

            tweetRequest.Headers.Add("Authorization", authorizationHeaderParams);

            string boundary = "======" +
                              Guid.NewGuid().ToString().Substring(18).Replace("-", "") +
                              "======";

            var separator = "--" + boundary;
            var footer = "\r\n" + separator + "--\r\n";

            string shortFileName = Path.GetFileName(imageFile);
            string fileContentType = GetMimeType(shortFileName);
            string fileHeader = string.Format("Content-Disposition: file; " +
                                              "name=\"media\"; filename=\"{0}\"",
                                              shortFileName);
            var encoding = System.Text.Encoding.GetEncoding("iso-8859-1");

            var contents = new System.Text.StringBuilder();
            contents.AppendLine(separator);
            contents.AppendLine("Content-Disposition: form-data; name=\"status\"");
            contents.AppendLine();
            contents.AppendLine(status);
            contents.AppendLine(separator);
            contents.AppendLine(fileHeader);
            contents.AppendLine(string.Format("Content-Type: {0}", fileContentType));
            contents.AppendLine();

            using (var s = tweetRequest.GetRequestStream())
            {
                byte[] bytes = encoding.GetBytes(contents.ToString());
                s.Write(bytes, 0, bytes.Length);
                bytes = File.ReadAllBytes(imageFile);
                s.Write(bytes, 0, bytes.Length);
                bytes = encoding.GetBytes(footer);
                s.Write(bytes, 0, bytes.Length);
            }

            tweetRequest.ContentType = "multipart/form-data; boundary=\"" + boundary + "\"";

            tweetRequest.Timeout = 5 * 60 * 1000;

            string responseFromServer = string.Empty;
            try
            {


                HttpWebResponse webResponse = tweetRequest.GetResponse() as HttpWebResponse;




                Stream dataStream = webResponse.GetResponseStream();

                StreamReader reader = new StreamReader(dataStream);

                responseFromServer = reader.ReadToEnd();

                // TwitterPostResponse result = JsonConvert.DeserializeObject<TwitterPostResponse>(responseFromServer);

            }
            catch (WebException webex)
            {
                throw webex;
                /*
                StreamReader exstream = new StreamReader(webex.Response.GetResponseStream());
                dynamic exceptionString = exstream.ReadToEnd();
                */
            }
            catch (Exception ex)
            {
            }
            return responseFromServer;
        }