I am trying to use the update profile image API via OAuth. This is how
I build my request.
Set the method as POST.
Set the content type as multipart/form-data; boundary="+boundary;
(Boundary is generated)
Write the OAuth parameters
oauth_consumer_key,oauth_nonce,oauth_signature,oauth_signature_method,oauth_timestamp,oauth_token,oauth_version
into the request stream.
I follow this up with
"--+boundary+\r\nContent-Disposition: form-data; name=\"image\";
filename=\"test.JPG}\"\r\nContent-Type: image/jpg\r\n\r\n".
This is followed by the byte stream of the image.
When I send this request to twitter, I receive 500(Internal server
error).
What am I doing wrong? Please help. I have been struggling since the
past week to get this working.
Please someone at least share the raw text of a successful request to
this API via OAuth. I will compare my request and see what I need to do
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
base
- 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
header)
{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
extension)]
{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
http://www.fiddlertool.com/dl/Fiddler1Setup.exe
- 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.
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)
- The best free tool for sniffing the HTTP requests happening for
your machine is Fiddler. You can download it from here
http://www.fiddlertool.com/dl/Fiddler1Setup.exe
How set the tile parameter is a question for which even I need find
answer for. Will update this space once something turns up.
if using OS X, and don't want to deal with raw tcpdump, then i can also recommend "packet peeper" (http://sourceforge.net/projects/packetpeeper/).