0

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

我已经从一组 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);
4

1 回答 1

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);
于 2013-05-07T14:04:31.007 回答