Photos.upload

From Facebook Developers Wiki

Jump to: navigation, search

Contents

[edit] Description

Uploads a photo owned by the specified user or the current session user and returns the new photo. See photo uploads for a description of the upload workflow. The only storable values returned from this call are pid, aid, and owner. All applications can upload photos with a "pending" state, which means that the photos must be approved by the user before they are visible on the site. Photos uploaded by applications with the photo_upload extended permission are visible immediately.

If no album is specified, the photo is uploaded to a default album for the application, which can be created if necessary. Regular albums have a size limit of 60 photos. Default application albums have a size limit of 1000 photos.

Important: Upload requests must be formed as a MIME multi-part message sent using POST data. Each argument, including the raw image data, should be specified as a separate chunk of form data.

The details of preparing a MIME multi-part message depend on your particular request library, but typically the library allows you to specify POST, set various MIME headers, then add additional data. The resulting MIME message should look like the example request below. Note that all line breaks in the message should be a carriage return followed by a newline ('\r\n') rather than just a newline ('\n'). For example, after specifying each Content-Disposition line, you should print '\r\n\r\n'.

It is strongly recommended that you scale the image in your application before adding it to the request. The largest dimension should be at most 604 pixels (the largest display size Facebook supports).

For Web applications, you must pass either the ID of the user on whose behalf you're making this call or the session key for that user, but not both. If you don't specify a user with the uid parameter, then that user whose session it is will be the target of the call.

However, if your application is a desktop application, you must pass a valid session key for security reasons. Do not pass a uid parameter.


[edit] Parameters

RequiredNameTypeDescription
required api_key string The application key associated with the calling application.
call_id float The request's sequence number. Each successive call for any session must use a sequence number greater than the last. We suggest using the current time in milliseconds, such as PHP's microtime(true) function.
sig string An MD5 hash of the current request and your secret key, as described in the authentication guide.
v string This must be set to 1.0 to use this version of the API.
[no name] data The raw image data for the photo.
optional session_key string The session key of the user whose photo you are uploading.
Note: A session key is always required for desktop applications. It is required for Web applications only when the uid is not specified.
format string Desired response format. Either XML (default) or JSON.
callback string Name of a function to call. This is primarily to enable cross-domain JavaScript requests using the <script> tag, sometimes known as "JSONP". This works with both XML and JSON.
aid int The album ID of the destination album.
caption string The caption of the photo.
uid int The user ID of the user whose photo you are uploading. If this parameter is not specified, then it defaults to the session user.
Note: This parameter applies only to Web applications and is required by them only if the session_key is not specified. Facebook ignores this parameter if it is passed by a desktop application.


[edit] Example Request

Content-Type: multipart/form-data; boundary=SoMeTeXtWeWiLlNeVeRsEe MIME-version: 1.0 --SoMeTeXtWeWiLlNeVeRsEe Content-Disposition: form-data; name="method" facebook.photos.upload --SoMeTeXtWeWiLlNeVeRsEe Content-Disposition: form-data; name="v" 1.0 --SoMeTeXtWeWiLlNeVeRsEe Content-Disposition: form-data; name="api_key" 77a52842357422fadd912a2600e6e53c --SoMeTeXtWeWiLlNeVeRsEe Content-Disposition: form-data; name="session_key" 489727d0ab2efc6e8003018c-i2LLkn8BDb2s. --SoMeTeXtWeWiLlNeVeRsEe Content-Disposition: form-data; name="call_id" 1172623588.023010 --SoMeTeXtWeWiLlNeVeRsEe Content-Disposition: form-data; name="caption" Under the sunset --SoMeTeXtWeWiLlNeVeRsEe Content-Disposition: form-data; name="aid" 940915667462717 --SoMeTeXtWeWiLlNeVeRsEe Content-Disposition: form-data; name="sig" dfa724b8a5cd97d9df4baf2b60d3484c --SoMeTeXtWeWiLlNeVeRsEe Content-Disposition: form-data; filename="somefilename.jpg" Content-Type: image/jpg [Raw file data here] --SoMeTeXtWeWiLlNeVeRsEe--


[edit] Example Return XML

<?xml version="1.0" encoding="UTF-8"?> <photos_upload_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd"> <pid>940915697041656</pid> <aid>940915667462717</aid> <owner>219074</owner> <src>http://ip002.facebook.com/v67/161/72/219074/s219074_31637752_5455.jpg</src> <src_big>http://ip002.facebook.com/v67/161/72/219074/n219074_31637752_5455.jpg</src_big> <src_small>http://ip002.facebook.com/v67/161/72/219074/t219074_31637752_5455.jpg</src_small> <link>http://www.facebook.com/photo.php?pid=31637752&id=219074</link> <caption>Under the sunset</caption> </photos_upload_response>


[edit] Error Codes

CodeDescription
1 An unknown error occurred. Please resubmit the request.
2 The service is not available at this time.
5 The request came from a remote address not allowed by this application.
100 One of the parameters specified was missing or invalid.
101 The API key submitted is not associated with any known application.
102 The session key was improperly submitted or has reached its timeout. Direct the user to log in again to obtain another key.
103 The submitted call_id was not greater than the previous call_id for this session.
104 Incorrect signature.
120 Invalid album ID.
200 The application does not have permission to operate on the passed in uid parameter.
321 Album is full.
324 Missing or invalid image file.
325 Too many unapproved photos pending.


[edit] Notes

  • This function does not require a session_key, unless it is being called by a desktop application.
Navigation