Question:
Good day. Is there any experience with uploading audio files to the server? I am doing this:
vk = VkontakteApi::Client.new(Rails.application.secrets.vkontakte_token)
audio = '/var/www/flat.fm/1.mp3'
# получить адрес сервера для загрузки аудиофайла
audio_upload_url = vk.audio.get_upload_server.upload_url
# загрузить на сервер
upload_audio = VkontakteApi.upload(url: audio_upload_url, audio: [audio, MIME::Types.type_for(audio).first.content_type])
Conclusion:
POST https://pu.vk.com/c613726/upload.php?act=add_audio&aid=0&api=1&gid=0&hash=0831f52f49516064cc4ce9b98fbc935d&mid=62703936&rhash=1b644f4030f359093033f2dca1efab04
body: #<Faraday::CompositeReadIO:0x000000054513c0 @parts=[#<Parts::FilePart:0x00000005451de8 @head="-------------RubyMultipartPost\r\nContent-Disposition: form-data; name=\"audio\"; filename=\"1.mp3\"\r\nContent-Length: 4931272\r\nContent-Type: application/octet-stream\r\nContent-Transfer-Encoding: binary\r\n\r\n", @foot="\r\n", @length=4931472, @io=#<CompositeReadIO:0x00000005451640 @ios=[#<StringIO:0x00000005451730>, #<UploadIO:0x00000005433a78 @content_type="application/octet-stream", @original_filename="1.mp3", @local_path="/var/www/flat.fm/1.mp3", @io=#<File:/var/www/flat.fm/1.mp3>, @opts={}>, #<StringIO:0x00000005451690>], @index=0>>, #<Parts::EpiloguePart:0x00000005451500 @part="-------------RubyMultipartPost--\r\n\r\n", @io=#<StringIO:0x00000005451460>>], @ios=[#<CompositeReadIO:0x00000005451640 @ios=[#<StringIO:0x00000005451730>, #<UploadIO:0x00000005433a78 @content_type="application/octet-stream", @original_filename="1.mp3", @local_path="/var/www/flat.fm/1.mp3", @io=#<File:/var/www/flat.fm/1.mp3>, @opts={}>, #<StringIO:0x00000005451690>], @index=0>, #<StringIO:0x00000005451460>], @index=0>
Answer:
#<Hashie::Mash audio="%7B%22error%22%3A-2%7D" hash="e1b4451767363d0599fdc913df64c274" redirect="http://vk.com/audio.php?act=done_add&mid=62703936&aid=0&gid=0&server=613726&audio=%7B%22error%22%3A-2%7D&hash=e1b4451767363d0599fdc913df64c274" server=613726>
Preservation:
save_audio = vk.audio.save(
server: upload_audio.server, audio: upload_audio.audio,
hash: upload_audio['hash'], artist: "1", title: '1'
)
Answer:
POST https://api.vk.com/method/audio.save?access_token=d...8
body: "artist=1&audio=%257B%2522error%2522%253A-2%257D&hash=e1b4451767363d0599fdc913df64c274&server=613726&title=1"
{"error":{"error_code":301,"error_msg":"Invalid filename","request_params":[{"key":"oauth","value":"1"},{"key":"method","value":"audio.save"},{"key":"artist","value":"1"},{"key":"audio","value":"%7B%22error%22%3A-2%7D"},{"key":"hash","value":"e1b4451767363d0599fdc913df64c274"},{"key":"server","value":"613726"},{"key":"title","value":"1"}]}}
VkontakteApi::Error: VkontakteApi::Error
The file name error, it is understandable %7B%22error%22%3A-2%7D
that it should not be so. But the question is: what's the matter?
Tried it with various files, various codecs of all kinds (including lame), ranging in size from 4 to 125 megabytes. I can't find the reason, help who knows what the problem is, thanks.
Answer:
Everything, figured out the error was here
# загрузить на сервер
upload_audio = VkontakteApi.upload(url: audio_upload_url, audio: [audio, MIME::Types.type_for(audio).first.content_type])
it is necessary to pass the file field and not audio :
upload_audio = VkontakteApi.upload(url: audio_upload_url, file: [audio, MIME::Types.type_for(audio).first.content_type])