我正在尝试从命令行在last.fm上的一个播放列表中添加“艺术家 - 歌曲”条目。
我申请了一个API密钥并获得了last.fm API documentation中描述的会话密钥。我使用user.getplaylist($ PLID)获得了播放列表ID。
该服务要求客户端将数据作为POST请求发送给写入任何内容的方法。为此,我决定使用curl -d
以下是add_track.sh的内容:
SERVICE=http://ws.audioscrobbler.com/2.0/
APIKEY=1dfdc3a278e6bac5c76442532fcd6a05 # mpc-last
SECRET=<md5hash: api secret>
LASTFM_USER=<string: myuser>
LASTFM_SK=<md5hash: valid session key>
# parameters (sorted alphabetically becasue method signature requires them to be)
# api_key (Required) : A Last.fm API key.
# api_sig (Required) : A Last.fm method signature. See authentication for more information.
# artist (Required) : The artist name that corresponds to the track to be added.
# method playlist.addTrack
# playlistID (Required) : The ID of the playlist - this is available in user.getPlaylists.
# sk (Required) : A session key generated by authenticating a user via the authentication protocol.
# track (Required) : The track name to add to the playlist.
METHOD=playlist.addtrack
ARTIST=prodigy
TRACK=breathe
PLID=8698647
MS="api_key${APIKEY}" # api_sig can't be here because it's not produced yet, obviously
MS="${MS}artist${ARTIST}" MS="${MS}method${METHOD}" MS="${MS}playlistid${PLID}" # tried with playlistID too
MS="${MS}track${TRACK}"
#MS="${MS}sk${LASTFM_SK}" # including this does not help
MS="${MS}${SECRET}"
# hash it
MS=`echo -n $MS | md5sum | cut -d' ' -f1`
# call the service.
# args also sorted alphabetically, but this should definitely not matter
curl \
-d api_key=${APIKEY} \
-d api_sig=${MS} \
-d artist=${ARTIST} \
-d method=${METHOD} \
-d playlistID=${PLID} \
-d sk=${LASTFM_SK} \
-d track=${TRACK} \
$SERVICE
然后我打电话给服务:
$ ./add_track.sh
<?xml version="1.0" encoding="utf-8"?>
<lfm status="failed">
<error code="13">Invalid method signature supplied</error></lfm>
答案 0 :(得分:0)
供参考:
curl -v \
-d "api_key=`perl -e 'use Encode; printf "%s", encode_utf8($ARGV[0])' ${APIKEY}`" \
-d "api_sig=`perl -e 'use Encode; printf "%s", encode_utf8($ARGV[0])' ${MS}`" \
-d "artist=`perl -e 'use Encode; printf "%s", encode_utf8($ARGV[0])' ${ARTIST}`" \
-d "method=`perl -e 'use Encode; printf "%s", encode_utf8($ARGV[0])' ${METHOD}`" \
-d "playlistID=`perl -e 'use Encode; printf "%s", encode_utf8($ARGV[0])' ${PLID}`" \
-d "sk=`perl -e 'use Encode; printf "%s", encode_utf8($ARGV[0])' ${LASTFM_SK}`" \
-d "track=`perl -e 'use Encode; printf "%s", encode_utf8($ARGV[0])' ${TRACK} `" \
$SE
.... 一个人可能最好从一开始就使用perl: http://www.easyclasspage.de/lastfm/seite-11.html