dongrendang6566 2017-09-21 16:03
浏览 46
已采纳

Spotify Web API - 缺少401权限

I'm trying to build a spotify web player using the spotify web API. I registered my Application on spotify and whitelist the callback URL. Then the authorization process works fine. I receive the token for make others requests. But when I try to make a simple currently-playing request,

https://developer.spotify.com/web-api/get-the-users-currently-playing-track/

I receive

Array ( [error] => Array ( [status] => 401 [message] => Permissions missing ) )

the PHP code is:

session_start();
$req = $_SESSION['token_type'] . " " . $_SESSION['token'];
$headers_after_token = array(
   "Accept: */*",
   "Authorization: " . $req);
$url="https://api.spotify.com/v1/me/player/currently-playing";
echo "<br>REQ-currently-playing: ".$req."<br>";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_after_token);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = json_decode(curl_exec($ch), true);
curl_close($ch);
echo "<br><br>";
print_r($response);

the $_SESSION['token_type'] contains "Bearer" as mentioned in the API endpoint-reference https://developer.spotify.com/web-api/endpoint-reference/

the $_SESSION['token'] contains the token retrieved after the authentication process.

Both are well formed thanks to the echo "<br>REQ-currently-playing: ".$req."<br>"; I can see that the 2 variables are set.

I'm using XAMPP v3.2.2 for deploy php pages.

  • 写回答

1条回答 默认 最新

  • dp19001 2017-09-22 16:15
    关注

    To be able to fetch the current playing, you need to add the scope user-read-currently-playing and/or user-read-playback-state when you authorize a user.

    With this kind of authorization, a user needs to agree what your app can do on their behalf. Some things are included by default, but some things (just like this) needs extra permissions from the user.

    If you see in the documentation that it says that a function needs "this and that" scope, you need to add it to the authorization.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)