2017-09-21 16:03
浏览 36

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,

I receive

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

the PHP code is:

$req = $_SESSION['token_type'] . " " . $_SESSION['token'];
$headers_after_token = array(
   "Accept: */*",
   "Authorization: " . $req);
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);
echo "<br><br>";

the $_SESSION['token_type'] contains "Bearer" as mentioned in the 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.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用spotify web API构建一个spotify网络播放器。 我在spotify上注册了我的应用程序 将回调URL列入白名单。 然后授权过程正常。 我收到了令其他人请求的令牌。 但是当我尝试制作一个简单的当前正在播放的请求时, < / a>


  Array([error] =&gt; Array([status] =&gt; 401 [message]  =&gt;缺少权限))


 $  req = $ _SESSION ['token_type']。  “”。  $ _SESSION ['token']; 
 $ headers_after_token = array(
“Accept:* / *”,
“授权:”。$ req); 
 $ url =“https://api.spotify。  com / v1 / me / player /当前播放“; 
echo”&lt; br&gt; REQ-正在播放:“。$ req。”&lt; br&gt;“; 
 $ 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“&lt; br&gt;&lt; br&gt;”; 
print_r($ response); 

$ _ SESSION ['token_type'] 包含API端点引用中提到的“Bearer”

< code> $ _ SESSION ['token'] 包含在身份验证过程之后检索到的令牌。

由于 echo“&lt; br&gt; REQ-current-playing:”。$ req。“&lt; br&gt;”; I 可以看到设置了2个变量。

我正在使用XAMPP v3.2.2来部署php页面。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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.

    打赏 评论

相关推荐 更多相似问题