Ok people. Finally managed to crack it. Thanks to Raffi for sharing
the raw text of the request. While working this API i figured out
there are very less resources available on Internet with regards to
the usage of multipart with OAuth and there is lot of confusion and
misleading data.

I will share what ever method worked for me with you people in a hope
that others will not have to go searching for the info again.

1. Method POST

2. The paramters which should be considered for the OAuth signature

      - Request method(.i.e POST in this case)
      - Encoded API Url(.i.e "http://twitter.com/account/
update_profile_background_image.format" in this case)
      - OAuth consumer key
      - OAuth nonce
      - OAuth Signature method
      - OAuth timesatmp
      - OAuth token
      - OAuth version

That is basically all the default OAuth parameters.Please note that
the "image" parameter should not be included.

3.  Where to place the OAuth parameters and the OAuth signature?

     It should be placed in the Authorisation header of the request.
Please look at the Authorisation header in the stream data attached by
Raffi in previous post for reference.

     Note you may have stuck the OAuth parameters in the request body
for other API requests. But it is absolutely necessary that you stick
them in to the Auth headers for
     this API.(Have to check the reason for this, will update this
space once i find something)

4.  Other headers which need to be set

    ContentType = "multipart/form-data; boundary="+boundary (this a
pre generated random alphanumeric value, please google out the way
this needs to be generated)

    Example boundary = "----------------------------645033dcf9bb"

    ContentLength = [Total length of the string in your request body
(This includes the byte array of the image data)]

5.  What should the request body look like?

     Let the final Request Body be = requestBody

     I shall divide this into 3 parts:

    Currently requestBody = ""

     Part 1:

     "--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=
\"{2}\"\r\nContent-Type: {3}\r\n\r\n"

     {0} = boundary(same as the one you attached in the ContentType
     {1} = "image"(this is essentially the form parameter whose data
you are sending as multipart, which in this case is "image")
     {2} = [The name of the image which you are sending(including the
     {3} = "image/[extension of the image you are uploading]", For
example "image/jpeg".

    Now your requestBody = Part 1

    Part 2:

    Get the binary Byte Stream of the image you are uploading, say
this Part 2.

    Now your requestBody = Part 1+Part 2.

    Part 3:

    "\r\n"+"--" + boundary(same as the one generated earlier) + "--"

    Your final requestBody =  Part 1+Part 2+Part 3.

This all I feel you need to know to get this API working. If you are
still facing issues. Then somethings which could help you debug the
issue are as follows:

     - Please compare the raw text of your request stream with the
request stream which Raffi has shared in the above post.
     - The best free tool for sniffing the HTTP requests happening for
your machine is Fiddler. You can download it from here
     - Please check the headers and OAuth signature.

How set the tile parameter is a question for which even I need find
answer for. Will update this space once something turns up.

Hope this helps all those people who are trying to build twitter API
library using OAuth.

Reply via email to