2017-08-08 42 views
0

我試圖使用curl訪問Spotify API。我可以通過終端在一個班輪內完成此工作,並且工作正常。例如:Bash腳本訪問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" 

這是我第一次在腳本中使用捲曲,所以也許我做錯了什麼?現在,當我運行腳本時,什麼都沒有發生。

編輯:

繼@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 

回答

1

這行看起來錯誤的,因爲它包括第一斜線,您的提示還包括

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

添加調試選項並檢查輸出在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"