SoundCloud桌面应用LibCurl:如何上传文件

SoundCloud Desktop App & LibCurl: How to Upload a File?

本文关键字:文件 桌面 应用 LibCurl SoundCloud      更新时间:2023-10-16

我用LibCurl在SoundCloud上上传文件到经过身份验证的用户帐户时遇到了严重的困难。

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

我一直在测试的文件是最简单的形式,由各种daw创建:PCM波,单声道和立体声,不同的长度但短(最多几秒钟-用于快速测试目的),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);

作为新手,我不理解变量"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);