【问题标题】:Soundcloud PUT API returning 401Soundcloud PUT API 返回 401
【发布时间】:2016-06-29 15:02:11
【问题描述】:

我正在尝试使用带有 CURL 的 HTTP API 更新 soundcloud 曲目详细信息。尽管我已经传递了我的客户 ID,但我收到 401 Unauthorized 错误作为响应。

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

回复是

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

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

【问题讨论】:

    标签: javascript api curl soundcloud


    【解决方案1】:

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

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

    编辑添加示例代码

    这里是 PUT 使用 Curl & 和 PHP 获取 soundcloud auth 令牌的示例。此代码来自一个有效的 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...
    }
    

    【讨论】:

    • 谢谢,但它对我不起作用,仍然显示错误 401。
    【解决方案2】:

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

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

    有关此 curl 选项的更多信息,请访问 this post

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-05
      • 1970-01-01
      • 2016-11-09
      • 2019-11-05
      • 1970-01-01
      • 2017-11-22
      • 1970-01-01
      相关资源
      最近更新 更多