Hi,

Can anyone please help, been at this for days?

I'm working with ASP.NET C# and trying to upload photo image to
Twitpic.

I am trying to use the OAuth procedure, but keep getting

"The remote server returned an error: (401) Unauthorized."


This is where I'm at.......

I have all of the following after initial authentication and
redirection to the upload image page:


oauth_token = XXXXXXXXXXXXX
oauth_token_secret = XXXXXXXXXXXXXXX
nonce = 7913223
consumerKey = XXXXXXXXXXXXXXX
consumerSecret = XXXXXXXXXXXXXXXXXXXXXXX
timeStamp = 1279301514
sig = IISiFDlCSRFu%2fcy7GrXaQ6HgI5c%3d


As I understand it, I then need to put this lot into a HttpWebRequest
using POST, along with the image data.


I have created two headers.........

"X-Verify-Credentials-Authorization:OAuth
oauth_token=XXXXXXXXXXXXX,realm=http://
api.twitter.com/,oauth_token_secret=XXXXXXXXXXXXX,oauth_consumer_key=XXXXXXXXXXXXXXXX,oauth_signature_method=HMAC-
SHA1,oauth_timestamp=1279301514,oauth_nonce=7913223,oauth_version=1.0,oauth_signature=IISiFDlCSRFu
%2fcy7GrXaQ6HgI5c%3d"


and..........

"X-Auth-Service-Provider:https://api.twitter.com/1/account/
verify_credentials.json"


This is my upload image code..........



private string UploadPhoto(byte[] binaryImageData, string ContentType,
string message, string filename, string _oAuth, string consumerKey,
string oauth_token, string timeStamp, string nonce, string sig)
    {
        string _response = string.Empty;

        string boundary = Guid.NewGuid().ToString().Replace("-", "");
        string requestUrl = "http://api.twitpic.com/2/upload.json";;
        HttpWebRequest request =
(HttpWebRequest)WebRequest.Create(requestUrl);
        string encoding = "iso-8859-1";

        request.PreAuthenticate = true;
        request.ContentType = string.Format("multipart/form-data;
boundary={0}", boundary);
        request.Method = "POST";

        string header = string.Format("--{0}", boundary);
        string footer = string.Format("--{0}--", boundary);

        StringBuilder contents = new StringBuilder();

        contents.AppendLine(header);
        contents.AppendLine(String.Format("Content-Disposition: form-
data; name=\"{0}\"", "X-Auth-Service-Provider"));
        contents.AppendLine();
        contents.AppendLine("https://api.twitter.com/1/account/
verify_credentials.json");

        contents.AppendLine(header);
        contents.AppendLine(String.Format("Content-Disposition: form-
data; name=\"{0}\"", "X-Verify-Credentials-Authorization"));
        contents.AppendLine();
        contents.AppendLine(_oAuth);

        contents.AppendLine(header);
        contents.AppendLine(String.Format("Content-Disposition: form-
data; name=\"{0}\"", "messsage"));
        contents.AppendLine();
        contents.AppendLine(message);

        contents.AppendLine(header);
        contents.AppendLine(String.Format("Content-Disposition: form-
data; name=\"{0}\"", "key"));
        contents.AppendLine();
        contents.AppendLine(consumerKey);


        contents.AppendLine(header);

        string fileContentType = ContentType;
        string fileHeader = String.Format("Content-Disposition: file;
name=\"{0}\"; filename=\"{1}\"", "media", filename);
        string fileData =
Encoding.GetEncoding(encoding).GetString(binaryImageData);

        contents.AppendLine(fileHeader);
        contents.AppendLine(String.Format("Content-Type: {0}",
fileContentType));
        contents.AppendLine();
        contents.AppendLine(fileData);

        contents.AppendLine(footer);

        byte[] bytes =
Encoding.GetEncoding(encoding).GetBytes(contents.ToString());
        request.ContentLength = bytes.Length;

        using (Stream requestStream = request.GetRequestStream())
        {
            requestStream.Write(bytes, 0, bytes.Length);

            using (HttpWebResponse response =
(HttpWebResponse)request.GetResponse())
            {
                using (StreamReader reader = new
StreamReader(response.GetResponseStream()))
                {
                    _response = reader.ReadToEnd();
                }
            }
        }
        return _response;
    }


Thanks in advance.

Reply via email to