【问题标题】:The Soundcloud Stream URL returns an error code 401Soundcloud Stream URL 返回错误代码 401
【发布时间】:2021-12-05 05:41:50
【问题描述】:

这个网址曾经在我的浏览器中运行良好,但现在它返回错误代码 401。

网址: 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
}

请协助我们解决此问题。提前谢谢你。

【问题讨论】:

    标签: audio oauth audio-streaming soundcloud http-status-code-401


    【解决方案1】:

    stream 方法需要一个授权头。

    # 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,了解如何申请访问令牌。

    【讨论】:

    • 非常感谢您的回复@DalmTo;但是,我不确定在哪里可以获得 OAuth ACCESS TOKEN;你能帮我解决这个问题吗?
    • 按照authentication guide 中的说明操作,它将向您展示如何请求访问令牌。 Oauth2 流程有三个步骤来请求访问令牌。文档甚至显示了在访问令牌过期时如何刷新它。
    • 感谢 DalmTo 它现在正在工作...... & 现在我面临 {"code":429,"message":"rate_limit_exceeded","link":"developers.soundcloud.com/docs/api/… - Too Many Requests", "errors":[{"error_message":"rate_limit_exceeded"}],"error":null,"error_code":"rate_limit_exceeded"} 我的 api 请求速率限制超出了如何提高
    • 这是一个不错的错误。意味着你要禁食。减慢您对许多请求和向服务器发送垃圾邮件的速度。
    • 是提供 参考这个stackoverflow.com/a/69672979/9596361@Toniq
    【解决方案2】:

    这是我的代码,我已经修复了返回错误代码 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:

    注意:生成的 Stream URL 只有效一次;如果你想要另一个,你必须使用未过期的 OAuth Token 生成一个新的。

    位置 - 流 URL 是您要查找的 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);
        ?>
    

    【讨论】:

    • 谢谢。您如何跟踪 access_token 的有效期?你是否使用 refresh_token ?
    • 访问令牌的有效期仅为 3600 秒。目前我没有使用刷新令牌?
    • 谢谢。如果track有download_url,你知道如何下载track吗?
    • 您想知道音轨是通过程序下载还是手动下载?您可以使用浏览器轻松手动下载。我正在使用 PHP 来自动下载。
    • 我想获取下载曲目的网址。可以分享一下下载track的代码吗?
    猜你喜欢
    • 1970-01-01
    • 2016-06-29
    • 1970-01-01
    • 2020-09-06
    • 1970-01-01
    • 2016-11-18
    • 2021-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多