Bash 访问 Spotify 的脚本 API -- curl 错误

Bash script to access Spotify API -- curl error

我正在尝试使用 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

添加调试选项并检查 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"

这一行看起来有误,因为它包含了您的提示中也包含的第一个斜杠

url=$"https://api.spotify.com/$endpoint"