Announcements

Missing token 401

Reply

Missing token 401

Casual Listener

I'm trying to call the method to add a track to liked songs (https://api.spotify.com/v1/me/tracks PUT).

 

I successfully receive a token with the correct scope required for the method: 

 

{
    "access_token": "BQCSsuOjuleHbd7Nee9V2cOFBIROMivTLfJjPo1VHNjOEfj-cx2GhZNVWwYPh9qcValR7QivYNTc1c8QQ4LySFiDpM8aTQ",
    "expires_in": 3600,
    "scope": "user-library-modify",
    "token_type": "Bearer"
}

 

 

But when I call the method with this token I receive 

 

 

{
  "error" : {
    "status" : 401,
    "message" : "Missing token"
  }
}

 

 

I tried to call the method with a new token inside Spotify Documentation Console and got the same error.

1 Reply

Re: Missing token 401

Casual Listener

The code you may be interesting in:

 

SPOTIFY_ACCESS_TOKEN="";

getAccessToken() {

SPOTIFY_TOKEN_URI="https://accounts.spotify.com/api/token";
if [ -z "${CLIENT_ID}" ]; then
    cecho "Invalid Client ID, please update ${USER_CONFIG_FILE}";
    showAPIHelp;
    return 1
fi
if [ -z "${CLIENT_SECRET}" ]; then
    cecho "Invalid Client Secret, please update ${USER_CONFIG_FILE}";
    showAPIHelp;
    return 1
fi
SHPOTIFY_CREDENTIALS=$(printf "${CLIENT_ID}:${CLIENT_SECRET}" | base64 | tr -d "\n"|tr -d '\r');

    cecho "Connecting to Spotify's API";

    SPOTIFY_TOKEN_RESPONSE_DATA=$( \
        curl "${SPOTIFY_TOKEN_URI}" \
            --silent \
            -X "POST" \
            -H "Authorization: Basic ${SHPOTIFY_CREDENTIALS}" \
            -d "grant_type=client_credentials&scope=user-library-modify" \
    )
    
    echo ${SPOTIFY_TOKEN_RESPONSE_DATA};
    
    if ! [[ "${SPOTIFY_TOKEN_RESPONSE_DATA}" =~ "access_token" ]]; then
        cecho "Autorization failed, please check ${USER_CONFG_FILE}"
        cecho "${SPOTIFY_TOKEN_RESPONSE_DATA}"
        showAPIHelp
        return 1
    fi
    
    SPOTIFY_ACCESS_TOKEN=$( \
        printf "${SPOTIFY_TOKEN_RESPONSE_DATA}" \
        | grep -E -o '"access_token":".*",' \
        | sed 's/"access_token"://g' \
        | sed 's/"//g' \
        | sed 's/,.*//g' \
    )
}

function like() {
getAccessToken;
            
            trackId=`osascript -e 'tell application "Spotify" to id of the current track' | sed -e 's/spotify:track:\(.*\)/\1/'`;

            
            SPOTIFY_LIKE_URI="https://api.spotify.com/v1/me/tracks?ids=${trackId}";
            cecho "like URL: ${SPOTIFY_LIKE_URI}";
            cecho "token: ${SPOTIFY_ACCESS_TOKEN}";
            
            results=$( \
                curl -i -X -s -G ${SPOTIFY_LIKE_URI} \
                -X "PUT" \
                -H "Authorization: Bearer ${SPOTIFY_ACCESS_TOKEN}" \
                -H "Accept: application/json" \
                -H "Content-Type: application/json"\
            )
}

 

SUGGESTED POSTS