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

放置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

编辑以添加示例代码

这是使用Curl和PHP进行Soundcloud身份验证令牌的PUT示例。这段代码来自一个正在运行的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...
}

09-25 17:22
查看更多