Update profile image API using OAuth

452 views
Skip to first unread message

Vikram

unread,
Jan 14, 2010, 7:09:46 AM1/14/10
to Twitter Development Talk
Hi all,

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.

Vikram

unread,
Jan 15, 2010, 12:20:42 PM1/15/10
to Twitter Development Talk
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

Raffi Krikorian

unread,
Jan 17, 2010, 7:00:25 PM1/17/10
to twitter-deve...@googlegroups.com
please see the thread at http://groups.google.com/group/twitter-development-talk/browse_thread/thread/86ee800b2d37d429 for more information.

also, please find attached a raw capture of a background image upload using OAuth.


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

--
Raffi Krikorian
Twitter Platform Team
http://twitter.com/raffi
update_profile_background_image.stream

Vikram

unread,
Jan 18, 2010, 11:20:04 AM1/18/10
to Twitter Development Talk
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
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.

Raffi Krikorian

unread,
Jan 18, 2010, 1:54:35 PM1/18/10
to twitter-deve...@googlegroups.com
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)

it is not absolutely necessary -- i personally like putting things in the header (section 5.4, 5.4.1, and 5.4.2 of the oauth specification) for two reasons.  
  1. clean separation of the application parameters versus the authorization parameters;
  2. intermediary servers may handle the contents of authorization headers differently than the contents of the POST (scrubbing from logs, caching, etc.)
but, you can stick them into the query parameters also (section a.5.3 of the oauth specification). 

    - 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

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/).
 
How set the tile parameter is a question for which even I need find
answer for. Will update this space once something turns up.

i'm attaching another raw dump of a conversation this time containing the tile parameter.  it is just another parameter of the form upload, and if you look at the attached stream, then you'll see 

------------------------------2e701b9768ba
Content-Disposition: form-data; name="tile"

true
------------------------------2e701b9768ba--

at the end of the upload.

-- 
update_profile_background_image_with_tile.stream

Jeff Enderwick

unread,
Jan 18, 2010, 2:12:58 PM1/18/10
to twitter-deve...@googlegroups.com
On Mon, Jan 18, 2010 at 10:54 AM, Raffi Krikorian <ra...@twitter.com> wrote:

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/).


I use wireshark for the same. tip: you may need to chmod/chown your /dev/bpf devices so that you (as a user) can read them. PP does this for you (and asks for your password).
Reply all
Reply to author
Forward
0 new messages