App examples
An example code is open and available on github
Uploading photos to the server is one of the tasks that are most difficult to implement when using Odnoklassniki’s API.
It consists of several steps, which we are now going to review.
Step 1 – getting a URL for uploading photos.
For this, the photosV2.getUploadUrl method is used. The method has several settings; you can learn more information about them in its description.
As the uploading is done for a specific user, we need either access_token or session_key, as well as the permission PHOTO_CONTENT from the user.
After requesting API we get the answer with future photo IDs, url for uploading and expires.
Having executed this method, we proceed to the next one.
In the example, this method is called by clicking on the “Draw” button. Once we have an url for uploading, we put it and draw a form.
Step 2 – uploading photos via HTTP POST
Photo uploading itself is done to upload_url from the first step.
The next part of the HTML code can be used to add 3 images:
Fillers
During the first step, you can request many photo ids with the photosV2.getUploadUrl method, but upload the content in smaller batches. For this, you should request a URL for adding with fillers, and then replace a filler with a corresponding value.
Filler | Must be replaced |
---|---|
${photoIds} | Must be replaced with a list of photo IDs that should be added. Photo IDs must be separated with commas (,). |
Response
The response contains a set of photo IDs, with a marker provided for each one.
Note
The method always replies in the JSON format
Possible errors
Error code | Cause |
---|---|
PHOTO_SIZE_LIMIT_EXCEEDED | The size of binary content of the image in bytes exceeds the limit |
PHOTO_SIZE_TOO_SMALL | Image size in pixels is too small |
PHOTO_SIZE_TOO_BIG | Image size in pixels is too big |
PHOTO_INVALID_FORMAT | Image format cannot be recognized |
PHOTO_IMAGE_CORRUPTED | Image format is recognized, but the content is corrupted |
PHOTO_NO_IMAGE | No image found in the request |
A full list of possible errors with codes can be viewed at Processing errors.
Examples
In this example, when an ajax request activates the “Send” button, photos are uploaded to a corresponding URL.
With the result received, it’s time to proceed to the next step.
When uploading photos to add a media topic, step 3 should be skipped.
You will need the token parameter, which you received in the response in step 2.
Step 3 – completing photo upload
Lastly, it is necessary to call the photosV2.commit method. It completes the photo upload and allows adding desirable meta information to photos (e.g. comments).
To use this method, you will need IDs of photos uploaded and token markers. You can find more information on this in the description of the method.
In this example, once data in the binary form is sent, an upload completion function is called.
To do this, we call photosV2.commit for the photos we got from step 2.
Having received the response, we check that the status of each photo upload is SUCCESS and show a notification about the successful upload. Otherwise, a message saying that uploading has failed.