简体   繁体   English

我正在使用 php 通过 PHP 和客户端凭据流连接到 spotify 的 API

[英]I am using php to connect to spotify's API with PHP and with the client credentials flow

I am new to using an sort of REST protocol and am having trouble getting my access token back from the cURL request.我不熟悉使用某种 REST 协议,并且无法从 cURL 请求中取回我的访问令牌。

This is using WAMP.这是使用 WAMP。 I have already enabled the cURL extension and that works now.我已经启用了 cURL 扩展,现在可以使用了。 I also have already made a spotify application and have my client ID and secret ID.我也已经制作了一个 spotify 应用程序并拥有我的客户端 ID 和秘密 ID。

<?php

/* Spotify Application Client ID and Secret Key */
$client_id     = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; 
$client_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

/* Get Spotify Authorization Token */
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,            'https://accounts.spotify.com/api/token' );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST,           1 );
curl_setopt($ch, CURLOPT_POSTFIELDS,     'grant_type=client_credentials' );
curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Authorization: Basic '.base64_encode($client_id.':'.$client_secret)));

$result=curl_exec($ch);
$json = json_decode($result, true);

 echo "Results of json:  ". + $json['access_token'];

/* Get Spotify Artist Photo */
echo "<pre>";
exec('curl -i "https://api.spotify.com/v1/search?<br>q=Maycon+%26+Vinicius+&limit=1&type=artist" -H "Accept: application/json" -H "Authorization: Bearer '.$json['access_token'].'" -H "Content-Type: application/json" 2>&1', $pp);
echo implode("\r\n", $pp);
?>

The error that I get is:我得到的错误是:

""status": 400, "message": "Only valid bearer authentication supported"" ""status": 400, "message": "仅支持有效承载认证""

When I try to print out the results of my json resource variable $json[access_token] it prints 0.当我尝试打印出我的 json 资源变量$json[access_token]的结果时,它会打印 0。

Here is the full error message.这是完整的错误消息。

HTTP/2 400 HTTP/2 400
www-authenticate: Bearer realm="spotify", error="invalid_request", www-authenticate: Bearer realm="spotify", error="invalid_request",
error_description="Only valid bearer authentication supported" error_description="仅支持有效的承载认证"
access-control-allow-origin: *访问控制允许来源:*
access-control-allow-headers: Accept, App-Platform, Authorization, Content- access-control-allow-headers:接受、应用平台、授权、内容-
Type, Origin, Retry-After, Spotify-App-Version类型、来源、重试后、Spotify-应用程序版本
access-control-allow-methods: GET, POST, OPTIONS, PUT, DELETE, PATCH访问控制允许方法:GET、POST、OPTIONS、PUT、DELETE、PATCH
access-control-allow-credentials: true访问控制允许凭据:真
access-control-max-age: 604800访问控制最大年龄:604800
content-type: application/json内容类型:应用程序/json
content-length: 99内容长度:99
date: Sat, 26 Oct 2019 15:55:39 GMT日期:2019 年 10 月 26 日星期六 15:55:39 GMT
via: 1.1 google通过:1.1 谷歌
alt-svc: clear alt-svc:清除

Have you tried other requests and run into similar authentication errors?您是否尝试过其他请求并遇到类似的身份验证错误? To test I took your query and simplified it a bit to test it on my system and it returned one artist, so it looks like it's working.为了进行测试,我接受了您的查询并对其进行了一些简化以在我的系统上对其进行测试,它返回了一位艺术家,因此它看起来可以正常工作。

curl -i "https://api.spotify.com/v1/search?q=Maycon+%26+Vinicius+&limit=1&type=artist" -H "Accept: application/json" -H "Authorization: Bearer $AccessToken" -H "Content-Type: application/json"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM