2013-05-06 43 views
0

我在使用LibCurl将一个文件上传到SoundCloud上经过身份验证的用户账户上存在严重困难。SoundCloud Desktop App&LibCurl:如何上传文件?

我已经得到了从一组JSON格式的字符串(感谢通过LibCurl调试回调)以下错误消息; ''error_message“:”无法上传该文件,确定它们是有效的声音文件?“”?'

我一直在测试的文件是最简单的形式,由各种DAW创建:PCM Wave,单声道和立体声,长短不一但短(最快几秒 - 用于快速测试),44.1 kHz采样率和16位深度。另外,我可以通过SoundCloud网站上的浏览器上传它们,无需抱怨。

是任何人都可以看看下面的代码,以帮助确定问题?

CURL* handle = curl_easy_init(); 
curl_slist* headers = nullptr; 
curl_httppost* formPost = nullptr; 
curl_httppost* lastPtr = nullptr; 

curl_easy_setopt (handle, CURLOPT_URL, "https://api.soundcloud.com/tracks.json"); 
curl_easy_setopt (handle, CURLOPT_USERAGENT, "libcurl-agent/1.0"); 
curl_easy_setopt (handle, CURLOPT_CAINFO, "(absolute path)/cacert.pem"); 
curl_easy_setopt (handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t) -1); 

headers = curl_slist_append (headers, "Expect: 100-continue"); 
headers = curl_slist_append (headers, "Content-type: multipart/form-data"); 

curl_formadd (&formPost, 
       &lastPtr, 
       CURLFORM_COPYNAME, "oauth_token", 
       CURLFORM_COPYCONTENTS, /*token*/, 
       CURLFORM_END); 

curl_formadd (&formPost, 
       &lastPtr, 
       CURLFORM_COPYNAME, "track[asset_data]", 
       CURLFORM_COPYCONTENTS, "Tone.wav", 
       CURLFORM_END); 

curl_formadd (&formPost, 
       &lastPtr, 
       CURLFORM_COPYNAME, "track[title]", 
       CURLFORM_COPYCONTENTS, "Tone", 
       CURLFORM_END); 

curl_formadd (&formPost, 
       &lastPtr, 
       CURLFORM_COPYNAME, "track[sharing]", 
       CURLFORM_COPYCONTENTS, "public", 
       CURLFORM_END); 

curl_formadd (&formPost, 
       &lastPtr, 
       CURLFORM_COPYNAME, "Tone.wav", 
       CURLFORM_FILE, "C:/440 Hz Tone - 3 Seconds.wav", 
       CURLFORM_END); 

curl_easy_setopt (handle, CURLOPT_HTTPHEADER, headers); 
curl_easy_setopt (handle, CURLOPT_HTTPPOST, formPost); 
curl_easy_setopt (handle, CURLOPT_POSTFIELDSIZE_LARGE, (curl_off_t) /* size of file in bytes */); 

curl_easy_setopt (handle, CURLOPT_VERBOSE, (long) 1); 
curl_easy_setopt (handle, CURLOPT_DEBUGFUNCTION, /*debug function*/); 
curl_easy_setopt (handle, CURLOPT_NOPROGRESS, (long) 0); 
curl_easy_setopt (handle, CURLOPT_PROGRESSFUNCTION, /*debug progress function*/); 

curl_easy_perform (handle); 

curl_easy_cleanup (handle); 
curl_formfree (formPost); 
curl_slist_free_all (headers); 

回答

0

作为新的这个东西,我不明白个变量“track [asset_data]”应该包含本地文件路径。这是固定的代码;

CURL* handle = curl_easy_init(); 
curl_slist* headers = nullptr; 
curl_httppost* formPost = nullptr; 
curl_httppost* lastPtr = nullptr; 

curl_easy_setopt (handle, CURLOPT_URL, "https://api.soundcloud.com/tracks.json"); 
curl_easy_setopt (handle, CURLOPT_USERAGENT, "libcurl-agent/1.0"); 
curl_easy_setopt (handle, CURLOPT_CAINFO, "(absolute path)/cacert.pem"); 
curl_easy_setopt (handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t) -1); 

headers = curl_slist_append (headers, "Expect: 100-continue"); 
headers = curl_slist_append (headers, "Content-type: multipart/form-data"); 

curl_formadd (&formPost, 
       &lastPtr, 
       CURLFORM_COPYNAME, "oauth_token", 
       CURLFORM_COPYCONTENTS, /*token*/, 
       CURLFORM_END); 

curl_formadd (&formPost, 
       &lastPtr, 
       CURLFORM_COPYNAME, "track[title]", 
       CURLFORM_COPYCONTENTS, "Tone", 
       CURLFORM_END); 

curl_formadd (&formPost, 
       &lastPtr, 
       CURLFORM_COPYNAME, "track[sharing]", 
       CURLFORM_COPYCONTENTS, "public", 
       CURLFORM_END); 

curl_formadd (&formPost, 
       &lastPtr, 
       CURLFORM_COPYNAME, "track[asset_data]", 
       CURLFORM_FILE, "C:/440 Hz Tone - 3 Seconds.wav", 
       CURLFORM_END); 

curl_easy_setopt (handle, CURLOPT_HTTPHEADER, headers); 
curl_easy_setopt (handle, CURLOPT_HTTPPOST, formPost); 
curl_easy_setopt (handle, CURLOPT_POSTFIELDSIZE_LARGE, (curl_off_t) /* size of file in bytes */); 

curl_easy_setopt (handle, CURLOPT_VERBOSE, (long) 1); 
curl_easy_setopt (handle, CURLOPT_DEBUGFUNCTION, /*debug function*/); 
curl_easy_setopt (handle, CURLOPT_NOPROGRESS, (long) 0); 
curl_easy_setopt (handle, CURLOPT_PROGRESSFUNCTION, /*debug progress function*/); 

curl_easy_perform (handle); 

curl_easy_cleanup (handle); 
curl_formfree (formPost); 
curl_slist_free_all (headers); 
相关问题