Soundcloud Stream URL returns 错误代码 401
The Soundcloud Stream URL returns an error code 401
这个 URL 曾经在我的浏览器中运行良好,但现在 returns 错误代码为 401。
URL: https://api.soundcloud.com/tracks/881102623/stream?client_id=fbb40e82698631328efb400b0700834f
浏览器响应:
{
"code": 401,
"message": "A request must contain the Authorization header. For details please refer to https://developers.soundcloud.com/blog/security-updates-api.",
"link": "https://developers.soundcloud.com/docs/api/explorer/open-api",
"status": "401 - Unauthorized",
"errors": [
{
"error_message": "A request must contain the Authorization header. For details please refer to https://developers.soundcloud.com/blog/security-updates-api."
}
],
"error": null
}
请协助我们解决此问题。提前谢谢你。
方法 stream 需要授权 header。
# request a track you want to stream
$ curl -X GET "https://api.soundcloud.com/tracks/TRACK_ID" \
-H "accept: application/json; charset=utf-8" \
-H "Authorization: OAuth ACCESS_TOKEN"
# extract stream_url when available from a response
$ curl -X GET "https://api.soundcloud.com/tracks/TRACK_ID/stream" \
-H "accept: application/json; charset=utf-8" \
-H "Authorization: OAuth ACCESS_TOKEN"
您正在获得 401 - Unauthorized
因为您没有提供访问令牌,具有访问轨道 881102623 的权限。
您应该参考 authentication guide 如何请求访问令牌。
这是我的代码,现在我已经修复了返回错误代码 401 的 Soundcloud Stream URL。它现在可以完全运行了。
<?php
/* SOUNDCLOUD CREDENTIALS */
$CLIENT_ID = 'YOUR_CLIENT_ID';
$CLIENT_SECRET = 'YOUR_CLIENT_SECRET';
$trackId = 1142143435; /*Your Track Id*/
$ACCESS_TOKEN = getOAuth($CLIENT_ID,$CLIENT_SECRET);
$stream = streamURL($trackId, $ACCESS_TOKEN);
$track_request = trackDetails($trackId, $ACCESS_TOKEN);
function getOAuth($CLIENT_ID,$CLIENT_SECRET)
{
$url = "https://api.soundcloud.com/oauth2/token";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Content-Type: application/x-www-form-urlencoded",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$data = "grant_type=client_credentials&client_id=".$CLIENT_ID."&client_secret=".$CLIENT_SECRET;
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
function trackDetails($trackId, $ACCESS_TOKEN)
{
$url = "https://api.soundcloud.com/tracks/" . $trackId;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"accept: application/json; charset=utf-8",
"Authorization: OAuth " . $ACCESS_TOKEN,
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
function streamURL($trackId, $ACCESS_TOKEN)
{
$url = "https://api.soundcloud.com/tracks/".$trackId."/stream";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"accept: application/json; charset=utf-8",
"Authorization: OAuth ".$ACCESS_TOKEN,
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
?>
我在这里包含了前面代码的 curl 响应。
对于 OAuth 令牌:
{
"access_token": "2-141361--bj15bZfizomSpwcfA5JX2ND",
"expires_in": 3599,
"refresh_token": "eP8P6FENUZ7WSGDIf3i1wvwGSwhHCfdx",
"scope": "",
"token_type": "bearer"
}
对于流 URL:
注意:生成的StreamURL只有效一次;如果您想要另一个,您必须使用未过期的 OAuth 令牌生成一个新的。
位置 - The Stream URL 就是您要找的那个。
{
"status": "302 - Found",
"location": "https://cf-media.sndcdn.com/GTbktMIdZVGG.128.mp3?Policy=eyJTdGF0ZW1lbnQiOlt7IlJlc291cmNlIjoiKjovL2NmLW1lZGlhLnNuZGNkbi5jb20vR1Ria3RNSWRaVkdHLjEyOC5tcDMqIiwiQ29uZGl0aW9uIjp7IkRhdGVMZXNzVGhhbiI6eyJBV1M6RXBvY2hUaW1lIjoxNjM0ODg3MjU3fX19XX0_&Signature=OMu7AQogYzdQpi0O0gLYvgn9IFvS5yPzRPxjYafLDDdEyKNvUxS3~TIZPC7IKOnBs~TPubz7DT~DDXkOKEVlkBDy0TxHD1LLKD3DIhE6ze8dHYXCGzlaqPCvkQ-51kDIwu8EeUFi5byK0X3UJqZ057AXhxX~qF9f5h~~5~LRbPR6ARcyWV97A-SeqZYugU6QA2T9G~JWF3fO3gLETWwplPuF0QhDARz~AIZ0AEG6xGWo6FhOPEMiBHJFa8IHCVgz2pzb-7Vvx4A1Wci2h2Nw4v6oT0Ao0WaF6-2gOOH67zl6So2vtdsMQEmvZjzaD~r5qaBkGJVWsSehBofiAUm2hg__&Key-Pair-Id=APKAI6TU7MMXM5DG6EPQ"
}
曲目详情:
{
"kind": "track",
"id": 1142143435,
"created_at": "2021/10/15 07:30:27 +0000",
"duration": 182831,
"commentable": true,
"comment_count": 0,
"sharing": "public",
"tag_list": " ",
"streamable": true,
"embeddable_by": "all",
"purchase_url": null,
"purchase_title": null,
"genre": "",
"title": "Track Name",
"description": "",
"label_name": null,
"release": null,
"key_signature": null,
"isrc": null,
"bpm": null,
"release_year": null,
"release_month": null,
"release_day": null,
"license": "all-rights-reserved",
"uri": "https://api.soundcloud.com/tracks/114214343435",
"user": {
"avatar_url": "https://i1.sndcdn.com/avatars-00014682366551-2cgqxk-large.jpg",
"id": 154405051,
"kind": "user",
"permalink_url": "https://soundcloud.com/account_name",
"uri": "https://api.soundcloud.com/users/15440503451",
"username": "User Name",
"permalink": "User Name",
"created_at": "2015/05/23 06:34:12 +0000",
"last_modified": "2018/11/21 10:41:15 +0000",
"first_name": null,
"last_name": null,
"full_name": "",
"city": null,
"description": "Om Tamil Calendar for your Android device! \nThis is the perfect and useful application to all Tamil speaking people & Tamil lovers!",
"country": null,
"track_count": 1030,
"public_favorites_count": 0,
"reposts_count": 0,
"followers_count": 0,
"followings_count": 0,
"plan": "Pro Unlimited",
"myspace_name": null,
"discogs_name": null,
"website_title": null,
"website": null,
"comments_count": 0,
"online": false,
"likes_count": 0,
"playlist_count": 25,
"subscriptions": [
{
"product": {
"id": "creator-pro-unlimited",
"name": "Pro Unlimited"
}
}
]
},
"permalink_url": "https://soundcloud.com/account_name/track_name",
"artwork_url": "https://i1.sndcdn.com/artworks-mMD5y9whMs3dyjarz-pTLUjw-lsarge.jpg",
"stream_url": "https://api.soundcloud.com/tracks/11421843435/stream",
"download_url": null,
"waveform_url": "https://wave.sndcdn.com/GTbktMdfIdZVGG_m.png",
"available_country_codes": null,
"secret_uri": null,
"user_favorite": null,
"user_playback_count": null,
"playback_count": 30,
"download_count": 0,
"favoritings_count": 0,
"reposts_count": 0,
"downloadable": false,
"access": "playable",
"policy": null,
"monetization_model": null
}
为流下载脚本 URL;
<?php
$soundcloudURL = 'SOUNDCLOUD_STREAM_URL';
$fileName = 'audio_1.mp3';
header("Cache-Control: private");
header("Content-type: audio/mpeg3");
header("Content-Transfer-Encoding: binary");
header("Content-Disposition: attachment; filename=".$fileName);
//So the browser can display the download progress
header("Content-Length: ".filesize($soundcloudURL));
readfile($soundcloudURL);
?>
这个 URL 曾经在我的浏览器中运行良好,但现在 returns 错误代码为 401。
URL: https://api.soundcloud.com/tracks/881102623/stream?client_id=fbb40e82698631328efb400b0700834f
浏览器响应:
{
"code": 401,
"message": "A request must contain the Authorization header. For details please refer to https://developers.soundcloud.com/blog/security-updates-api.",
"link": "https://developers.soundcloud.com/docs/api/explorer/open-api",
"status": "401 - Unauthorized",
"errors": [
{
"error_message": "A request must contain the Authorization header. For details please refer to https://developers.soundcloud.com/blog/security-updates-api."
}
],
"error": null
}
请协助我们解决此问题。提前谢谢你。
方法 stream 需要授权 header。
# request a track you want to stream
$ curl -X GET "https://api.soundcloud.com/tracks/TRACK_ID" \
-H "accept: application/json; charset=utf-8" \
-H "Authorization: OAuth ACCESS_TOKEN"
# extract stream_url when available from a response
$ curl -X GET "https://api.soundcloud.com/tracks/TRACK_ID/stream" \
-H "accept: application/json; charset=utf-8" \
-H "Authorization: OAuth ACCESS_TOKEN"
您正在获得 401 - Unauthorized
因为您没有提供访问令牌,具有访问轨道 881102623 的权限。
您应该参考 authentication guide 如何请求访问令牌。
这是我的代码,现在我已经修复了返回错误代码 401 的 Soundcloud Stream URL。它现在可以完全运行了。
<?php
/* SOUNDCLOUD CREDENTIALS */
$CLIENT_ID = 'YOUR_CLIENT_ID';
$CLIENT_SECRET = 'YOUR_CLIENT_SECRET';
$trackId = 1142143435; /*Your Track Id*/
$ACCESS_TOKEN = getOAuth($CLIENT_ID,$CLIENT_SECRET);
$stream = streamURL($trackId, $ACCESS_TOKEN);
$track_request = trackDetails($trackId, $ACCESS_TOKEN);
function getOAuth($CLIENT_ID,$CLIENT_SECRET)
{
$url = "https://api.soundcloud.com/oauth2/token";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Content-Type: application/x-www-form-urlencoded",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$data = "grant_type=client_credentials&client_id=".$CLIENT_ID."&client_secret=".$CLIENT_SECRET;
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
function trackDetails($trackId, $ACCESS_TOKEN)
{
$url = "https://api.soundcloud.com/tracks/" . $trackId;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"accept: application/json; charset=utf-8",
"Authorization: OAuth " . $ACCESS_TOKEN,
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
function streamURL($trackId, $ACCESS_TOKEN)
{
$url = "https://api.soundcloud.com/tracks/".$trackId."/stream";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"accept: application/json; charset=utf-8",
"Authorization: OAuth ".$ACCESS_TOKEN,
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
?>
我在这里包含了前面代码的 curl 响应。
对于 OAuth 令牌:
{
"access_token": "2-141361--bj15bZfizomSpwcfA5JX2ND",
"expires_in": 3599,
"refresh_token": "eP8P6FENUZ7WSGDIf3i1wvwGSwhHCfdx",
"scope": "",
"token_type": "bearer"
}
对于流 URL:
注意:生成的StreamURL只有效一次;如果您想要另一个,您必须使用未过期的 OAuth 令牌生成一个新的。
位置 - The Stream URL 就是您要找的那个。
{
"status": "302 - Found",
"location": "https://cf-media.sndcdn.com/GTbktMIdZVGG.128.mp3?Policy=eyJTdGF0ZW1lbnQiOlt7IlJlc291cmNlIjoiKjovL2NmLW1lZGlhLnNuZGNkbi5jb20vR1Ria3RNSWRaVkdHLjEyOC5tcDMqIiwiQ29uZGl0aW9uIjp7IkRhdGVMZXNzVGhhbiI6eyJBV1M6RXBvY2hUaW1lIjoxNjM0ODg3MjU3fX19XX0_&Signature=OMu7AQogYzdQpi0O0gLYvgn9IFvS5yPzRPxjYafLDDdEyKNvUxS3~TIZPC7IKOnBs~TPubz7DT~DDXkOKEVlkBDy0TxHD1LLKD3DIhE6ze8dHYXCGzlaqPCvkQ-51kDIwu8EeUFi5byK0X3UJqZ057AXhxX~qF9f5h~~5~LRbPR6ARcyWV97A-SeqZYugU6QA2T9G~JWF3fO3gLETWwplPuF0QhDARz~AIZ0AEG6xGWo6FhOPEMiBHJFa8IHCVgz2pzb-7Vvx4A1Wci2h2Nw4v6oT0Ao0WaF6-2gOOH67zl6So2vtdsMQEmvZjzaD~r5qaBkGJVWsSehBofiAUm2hg__&Key-Pair-Id=APKAI6TU7MMXM5DG6EPQ"
}
曲目详情:
{
"kind": "track",
"id": 1142143435,
"created_at": "2021/10/15 07:30:27 +0000",
"duration": 182831,
"commentable": true,
"comment_count": 0,
"sharing": "public",
"tag_list": " ",
"streamable": true,
"embeddable_by": "all",
"purchase_url": null,
"purchase_title": null,
"genre": "",
"title": "Track Name",
"description": "",
"label_name": null,
"release": null,
"key_signature": null,
"isrc": null,
"bpm": null,
"release_year": null,
"release_month": null,
"release_day": null,
"license": "all-rights-reserved",
"uri": "https://api.soundcloud.com/tracks/114214343435",
"user": {
"avatar_url": "https://i1.sndcdn.com/avatars-00014682366551-2cgqxk-large.jpg",
"id": 154405051,
"kind": "user",
"permalink_url": "https://soundcloud.com/account_name",
"uri": "https://api.soundcloud.com/users/15440503451",
"username": "User Name",
"permalink": "User Name",
"created_at": "2015/05/23 06:34:12 +0000",
"last_modified": "2018/11/21 10:41:15 +0000",
"first_name": null,
"last_name": null,
"full_name": "",
"city": null,
"description": "Om Tamil Calendar for your Android device! \nThis is the perfect and useful application to all Tamil speaking people & Tamil lovers!",
"country": null,
"track_count": 1030,
"public_favorites_count": 0,
"reposts_count": 0,
"followers_count": 0,
"followings_count": 0,
"plan": "Pro Unlimited",
"myspace_name": null,
"discogs_name": null,
"website_title": null,
"website": null,
"comments_count": 0,
"online": false,
"likes_count": 0,
"playlist_count": 25,
"subscriptions": [
{
"product": {
"id": "creator-pro-unlimited",
"name": "Pro Unlimited"
}
}
]
},
"permalink_url": "https://soundcloud.com/account_name/track_name",
"artwork_url": "https://i1.sndcdn.com/artworks-mMD5y9whMs3dyjarz-pTLUjw-lsarge.jpg",
"stream_url": "https://api.soundcloud.com/tracks/11421843435/stream",
"download_url": null,
"waveform_url": "https://wave.sndcdn.com/GTbktMdfIdZVGG_m.png",
"available_country_codes": null,
"secret_uri": null,
"user_favorite": null,
"user_playback_count": null,
"playback_count": 30,
"download_count": 0,
"favoritings_count": 0,
"reposts_count": 0,
"downloadable": false,
"access": "playable",
"policy": null,
"monetization_model": null
}
为流下载脚本 URL;
<?php
$soundcloudURL = 'SOUNDCLOUD_STREAM_URL';
$fileName = 'audio_1.mp3';
header("Cache-Control: private");
header("Content-type: audio/mpeg3");
header("Content-Transfer-Encoding: binary");
header("Content-Disposition: attachment; filename=".$fileName);
//So the browser can display the download progress
header("Content-Length: ".filesize($soundcloudURL));
readfile($soundcloudURL);
?>