[英]Spotify API auth in PHP - Invalid redirect URI
I'm trying to connect to the Spotify API.我正在尝试连接到 Spotify API。
I get the authorization code with:我通过以下方式获得授权码:
<?php
$redirect_uri = 'http%3A%2F%2Flocalhost%3A8000';
?>
<a href="https://accounts.spotify.com/authorize?client_id=<?php echo $client_id;?>&response_type=code&redirect_uri=<?php echo $redirect_uri; ?>&scope=user-read-private%20user-read-email&state=34fFs29kd09">Get code</a><br/><br/>
So far so good, I get the code.到目前为止一切顺利,我得到了代码。 Then I try to exchange for a token with:
然后我尝试用以下方式交换令牌:
$redirect_uri = 'http%3A%2F%2Flocalhost%3A8000';
$url = 'https://accounts.spotify.com/api/token';
$fields = [
'grant_type' => 'authorization_code',
'code' => $code,
'redirect_uri' => $redirect_uri,
'client_id' => $client_id,
'secret' => $secret
];
$fields_string = http_build_query($fields);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
$result = curl_exec($ch);
And I've got every variation I can imagine of http://localhost:8000 whitelisted in the Spotify dashboard:我在 Spotify 仪表板中将 http://localhost:8000的所有变体都列入白名单:
But I get this error:但我得到这个错误:
result: {"error":"invalid_grant","error_description":"Invalid redirect URI"}
edit: What's weird is I CAN successfully link up with the implicit grant client side method, using the redirect URI http:%2F%2Flocalhost%3A8000 - so I know that this is whitelisted properly.编辑:奇怪的是,我可以使用重定向 URI http:%2F%2Flocalhost%3A8000 成功链接到隐式授权客户端方法 - 所以我知道这已正确列入白名单。 I've used this URI in the code I posted above, and get the same error.
我在上面发布的代码中使用了这个 URI,得到了同样的错误。 I've also used every other combination I can think of, whether that's using:%2F%2F, %3A%2F%2F, a trailing slash, a trailing %3A etc etc. Same error every time!
我还使用了我能想到的所有其他组合,无论是使用:%2F%2F、%3A%2F%2F、斜杠、%3A 等。每次都出现同样的错误!
Any ideas?有任何想法吗?
edit2: if I use $redirect_uri = ' http://localhost:8000 ';编辑2:如果我使用 $redirect_uri = ' http://localhost:8000 '; i get a different error:
我得到一个不同的错误:
result: {"error":"invalid_request","error_description":""}
Now that you have stopped encoding the redirect_uri, it is complaining about invalid parameters.现在您已经停止对 redirect_uri 进行编码,它正在抱怨无效参数。
As per the documentation, the client_id and secret aren't meant to sent along with the other parameters, they need to be added to the headers via the Authorization header:根据文档,client_id 和 secret 并不意味着与其他参数一起发送,它们需要通过 Authorization header 添加到标头中:
https://developer.spotify.com/documentation/general/guides/authorization-guide/#2-have-your-application-request-refresh-and-access-tokens-spotify-returns-access-and-refresh-tokens https://developer.spotify.com/documentation/general/guides/authorization-guide/#2-have-your-application-request-refresh-and-access-tokens-spotify-returns-access-and-refresh-tokens
HEADER PARAMETER Authorization Base 64 encoded string that contains the client ID and client secret key. The field must have the format: Authorization: Basic <base64 encoded client_id:client_secret>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.