我正在尝试使用 curl 访问 Spotify API。我可以在终端的一个衬里中执行此操作,并且工作正常。例如:
curl -X GET "https://api.spotify.com/v1/tracks/2vEQ9zBiwbAVXzS2SOxodY" -H "Authorization: Bearer <mytoken>"
但是,当我尝试将其嵌入到 bash 脚本中时,我没有得到任何输出。这是我的 bash 脚本:
#!/bin/sh
# For more info about endpoint references, visit:
# https://developer.spotify.com/web-api/endpoint-reference/
token=$(./spotifyAccess.php | jq '.access_token' | sed 's/"//g') # where spotifyAccess.php genereates my access token
read -p "Please enter a method of authentification (e.g. GET, PUT, POST) " method
read -p "Please enter an endpoint (e.g. /v1/audio-features/{id}) " endpoint
read -p "Please enter a Spotify ID (e.g.2vEQ9zBiwbAVXzS2SOxodY) " id
url=$"https://api.spotify.com/$endpoint"
url=$(echo $url | sed "s/{id}/$id/g")
echo "My URl is: $url"
curl -X $method $url -H "Authorization: Bearer $token"
这是我第一次在脚本中使用 curl,所以也许我做错了什么?现在,当我运行脚本时,没有任何反应。
编辑:
按照@skr建议,我将调试选项set -x
添加到我的脚本中。输出如下:
HTTP/1.1 404 Not Found
Server: nginx
Date: Tue, 08 Aug 2017 21:19:05 GMT
Content-Length: 0
Connection: keep-alive
Keep-Alive: timeout=600
Cache-Control: private, max-age=0
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE
Access-Control-Allow-Credentials: true
Access-Control-Max-Age: 604800
Access-Control-Allow-Headers: Accept, Authorization, Origin, Content-Type
此行看起来是错误的,因为它包含提示符还包含的第一个斜杠
url=$"https://api.spotify.com/$endpoint"
添加调试选项并检查 bash 脚本中的输出。
#!/bin/sh
#debug option
set -x
# For more info about endpoint references, visit:
# https://developer.spotify.com/web-api/endpoint-reference/
token=$(./spotifyAccess.php | jq '.access_token' | sed 's/"//g') # where spotifyAccess.php genereates my access token
read -p "Please enter a method of authentification (e.g. GET, PUT, POST) " method
read -p "Please enter an endpoint (e.g. /v1/audio-features/{id}) " endpoint
read -p "Please enter a Spotify ID (e.g.2vEQ9zBiwbAVXzS2SOxodY) " id
url=$"https://api.spotify.com/$endpoint"
url=$(echo $url | sed "s/{id}/$id/g")
echo "My URl is: $url"
curl -X $method $url -H "Authorization: Bearer $token"