Twitter API Chunked Upload INIT authentication error (code 32)

70 views Asked by At

Im having trouble getting the INIT post to https://upload.twitter.com/1.1/media/upload.json to authenticate. It's returning "message":"Could not authenticate you","code":32

I have the simple upload for images working fine, and can publish messages too, and im using the same code to create the Authorazation header etc. The auth header is as follows (with my consumer key and auth token)

OAuth oauth_consumer_key="[my consumer key]", oauth_token="[my auth token]", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1671623331", oauth_nonce="NjM4MDcyMjAxMzExODczNDAx", oauth_version="1.0", oauth_signature="NqC6Fwrz763C8397%2FL67crijtZs%3D"

I've tried passing in the required values (command, total_bytes and media_type) in the body and on the query string, and i've tried including them when generating the signature too. I just cant see the cause of the issue.

     var baseFormat_pic = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&command={6}&total_bytes={7}&media_type={8}";
     var baseString_pic = string.Format(baseFormat_pic,
                          oauth_consumer_key,
                          oauth_nonce,
                          oauth_signature_method,
                          oauth_timestamp,
                          oauth_token,
                          oauth_version,
                          "INIT",
                          fileBytes.ToString(),
                          HttpUtility.UrlEncode( "video/mp4"));

         baseString_pic = string.Concat("POST&", Uri.EscapeDataString(resource_url_pic),
                                         "&", Uri.EscapeDataString(baseString_pic));
         var compositeKey_pic = string.Concat(Uri.EscapeDataString(oauth_consumer_secret),
                                            "&", Uri.EscapeDataString(oauth_token_secret));

         string oauth_signature_pic;
         using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey_pic)))
         {
             oauth_signature_pic = Convert.ToBase64String(hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString_pic)));
         }

         var headerFormat_pic = "OAuth oauth_consumer_key=\"{3}\", oauth_token=\"{4}\", oauth_signature_method=\"{1}\", oauth_timestamp=\"{2}\", oauth_nonce=\"{0}\", oauth_version=\"{6}\", oauth_signature=\"{5}\"";

         var authHeader_pic = string.Format(headerFormat_pic,
                                            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_pic),
                                            Uri.EscapeDataString(oauth_version)
                                            );
         ServicePointManager.Expect100Continue = false;
         string reqUrl = resource_url_pic; 
         HttpWebRequest request = (HttpWebRequest)WebRequest.Create(reqUrl);
         request.Method = "POST";
                           
         NameValueCollection nvc = new NameValueCollection();
         nvc.Add("command", "INIT");
         nvc.Add("total_bytes", fileBytes.ToString());
         nvc.Add("media_type", "video/mp4");

         var sb = new StringBuilder();
         foreach (string key in nvc.Keys)
         {
              sb.AppendFormat("{0}={1}&", key, HttpUtility.UrlEncode(nvc[key].ToString()));
         }
         sb.Remove(sb.Length - 1, 1);
                           
         var bytes = Encoding.UTF8.GetBytes(sb.ToString());
         request.ContentLength = bytes.Length;
         request.Headers.Add("Authorization", authHeader_pic);
         request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
         var stream = request.GetRequestStream();
         stream.Write(bytes, 0, bytes.Length);
         stream.Close();
         string result = ReadResult(request);

Am I missing something really obvious?

1

There are 1 answers

0
DorrisDormouse On BEST ANSWER

To solve this I changed from form url encoded to multipart form data. This allowed the INIT command to work correctly.