M
M
maaGames2015-06-21 13:05:15
In contact with
maaGames, 2015-06-21 13:05:15

How to upload a photo to the VK server (c++, curl)?

I know that the question is raised regularly, but in PHP. I suffer in stand-alone in C++. Under Windows. Without QT.
Purpose: uploading photos from a local computer to my VK page
Trying:
1. Authorization and obtaining rights - everything is fine. I managed without web kits and other things, I stupidly authenticate through IWebBrowser2. Everything is OK, I can write text messages on my wall, add links...
2. https://api.vk.com/method/photos.getWallUploadServer
Everything is fine here too, returns json from upload_url
3. Finished great. No matter how I tried to send a request by upload_url, I either have an empty "photo":"[]" field in the response, or a protection error (when I tried to form a header).
I didn’t get to save the photo, as you know.
Tried to send both "photo=FULL_PATH TO_FILE_ON DISK" and "photo= http://url... " and "photo=ENCODED_base64_PICTURE" in POST - the result is constant.
I understand that I am doing something wrong, but VK does not return any error messages. I suspect I need to do some special shaping of CURLOPT_HTTPHEADER, but I can only make it worse...
Can anyone explain to me what I'm doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-06-23
@maaGames

This is how the request looks like (although I don’t upload it to the wall, but to the album, but the essence is the same)

POST http://cs62xxxx.vk.com/upload.php?act=do_add&mid=6xxxxxxx&aid=213651645&gid=0&hash=1b88ccc2a73467387614fdc740241ec3&rhash=af7e667ec013e00d6b7a42234018xxxx&swfupload=1&api=1 HTTP/1.1
Host: cs62xxxx.vk.com
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0
Accept: */*
Accept-Encoding: deflate, gzip
Connection: Keep-Alive
Cookie: remixlang=0
Content-Length: 12573
Content-Type: multipart/form-data; boundary=------------------------23512563ad262e45

--------------------------23512563ad262e45
Content-Disposition: form-data; name="file1"; filename="screenshot 2015-03-09 001_en.png"
Content-Type: image/png
 PNG....содержимое файла
--------------------------23512563ad262e45--

it is done roughly like this:
struct curl_httppost *formpost=NULL;
struct curl_httppost *lastptr=NULL;

curl_formadd(&formpost,
                            &lastptr,
                            CURLFORM_COPYNAME, "file1",
                            CURLFORM_FILENAME, "screenshot 2015-03-09 001_en.png",
                            CURLFORM_FILE, "D:\\screenshot 2015-03-09 001_en.png",
                            CURLFORM_CONTENTTYPE, "image/png",
                            CURLFORM_END);
 curl_easy_setopt(curl_handle, CURLOPT_URL, upload_url);
 curl_easy_setopt(curl_handle, CURLOPT_HTTPPOST, formpost);
 curl_result = curl_easy_perform(curl_handle);

In response, you get json with data that will need to be transferred to save the photo.
Taken from here https://github.com/zenden2k/image-uploader/blob/ma...
it's not C++ but the underlying curl calls ( https://github.com/zenden2k/image-uploader/blob/1.. .. )
If you want, you can use my wrapper in your project, so as not to suffer with the sish api
https://github.com/zenden2k/curl-cpp-wrapper

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question