Soundcloud PUT API returning 401

Soundcloud PUT API returning 401

本文关键字:returning API PUT Soundcloud      更新时间:2023-09-26

我正在尝试使用带有CURL的HTTP API更新soundcloud曲目详细信息。我收到 401 未经授权的错误作为响应,即使我已经通过了我的客户端 ID。

放 https://api.soundcloud.com/tracks/11111111?client_id=12345666666

响应是

{
  "errors": [
    {
      "error_message": "401 - Unauthorized"
    }
  ]
}

还想知道我是否可以将请求传递给access_token。

如果你想传递你的令牌,只需附加"&oauth_token="+TOKEN_VALUE

https://api.soundcloud.com/tracks/11111111?client_id=12345666666&oauth_token=YOUR_TOKEN

已编辑以添加示例代码

下面是一个PUT使用Curl和PHP作为soundcloud身份验证令牌的示例。此代码来自一个有效的 soundcloud 项目。

$data = array(
  'code' => $token,
  'client_id' => $this->getClientId(false), // your client ID
  'client_secret' => $this->getClientSecret(), // your client secret
  'redirect_uri' => $this->getCallback(), // callback URL
  'grant_type' => 'authorization_code'
);
$url = "https://api.soundcloud.com/oauth2/token";
try {
  $ch = curl_init();
  // set options
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_HEADER, true);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  curl_setopt($ch, CURLOPT_VERBOSE, 1);
  $response = curl_exec($ch);
  $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  $header = substr($response, 0, $header_size);
  $body = substr($response, $header_size);      
  // read / process result
  curl_close($ch);
} catch(Exception  $e) {
  // error handling...
}

这可能有效。尝试关闭各种 CURL 选项。就我而言,我在 PHP5.5 下工作了 PUT,但在迁移到带有 PHP7 的新服务器后,POST 操作将失败,直到我将 CURLOPT_SAFE_UPLOAD 设置为 false 进行声云 POST 操作(文件上传);但是,在对我的所有 curl 初始化进行此更改后,我的 PUT 操作失败了,因为它们也将 safeupload 设置为 false,所以我从我的 PUT 操作中删除了此设置,PUT 操作开始工作。

简而言之,请尝试关闭 PUT 操作的安全上传选项。

有关此卷曲选项的更多信息,请参阅这篇文章。