不能跟踪(批量)到Last.fm API。方法签名无效

时间:2013-09-16 20:49:38

标签: javascript jquery last.fm

我已经尝试了一段时间了,但是我无法理解错误。也许我已经尝试了很多方法,我甚至不确定这段代码是否正确。

基本上我正在尝试使用Last.fm API中的track.scrobble方法,发送一批曲目。

这就是我的代码,它总是返回Invalid method signature。有人能在这里给我一些帮助吗?

更新

根据 mccannf 回答,我更改了代码,但仍然收到错误:

var apiUrl = "http://ws.audioscrobbler.com/2.0/";
var apiMethod = "track.scrobble";
var apiKey = "MY_API_KEY";
var apiSecret = "MY_API_SECRET";
var key = "MY_SESSION_KEY";
var apiSig = "";

var lastfmScrobble = function (data) {
    var dataToScrobble = setTiming(data);

    var albums = [];
    var artists = [];;
    var timestamps = [];
    var tracks = [];
    var dataToHash = "";

    for (var i = 0; i < dataToScrobble.tracks.length; i++) {
        albums["album["+ i.toString() + "]"] = dataToScrobble.album;        
        artists["artist[" + i.toString() + "]"] = dataToScrobble.artist;
        timestamps["timestamp[" + i.toString() + "]"] = dataToScrobble.tracks[i].split("|")[1];
        tracks["track[" + i.toString() + "]"] = dataToScrobble.tracks[i].split("|")[0];     
    }

    dataToHash += albums.sort().join("");
    dataToHash += "api_key" + apiKey;   
    dataToHash += artists.sort().join("");  
    dataToHash += "method" + apiMethod;
    dataToHash += "sk" + key;   
    dataToHash += timestamps.sort().join("");       
    dataToHash += tracks.sort().join("");
    dataToHash += apiSecret;

    apiSig = $.md5(unescape(encodeURIComponent(dataToHash)));

    var songsToScrobble = {};

    $.extend(songsToScrobble, 
        albums.sort(), 
        { api_key: apiKey }, 
        { api_sig: apiSig }, 
        artists.sort(), 
        { method: apiMethod }, 
        { sk: key }, 
        timestamps.sort(), 
        tracks.sort());

    $.ajax({
        url: apiUrl,
        type: "POST",
        data: songsToScrobble,
        success: function (data) { 
            console.log(data);
        }
    });
}

现在发送的对象具有正确的格式(JSON)。还有什么可能是错的?

1 个答案:

答案 0 :(得分:0)

我做了一个快速的样本JS Fiddle of your code

dataToHash是这样的:

 album[0]Achtung Babyalbum[1]Achtung Babyapi_keyxxxBLAHxxxartist[0]U2artist[1]U2methodtrack.scrobbleskkkkFOOkkktimestamp[0]1379368800timestamp[1]1379369000track[0]Onetrack[1]The FlymmmySecrettt 

上面代码中的songsToScrobble变量如下所示:

 { "album": [ 
              "album[0]Achtung Baby",
              "album[1]Achtung Baby"
            ], 
   "api_key":"xxxBLAHxxx",
   "api_sig":"8dbc147e533411a41ba9169f59e65b3a",
   "artist":["artist[0]U2","artist[1]U2"],          
   "method": "track.scrobble", 
   "sk":"kkkFOOkkk" 
   "timestamp": [
                  "timestamp[0]1379368800",
                  "timestamp[1]1379369000"
                ],
   "track": [ 
              "track[0]One",
              "track[1]The Fly"
            ]
  } 

相信 songsToScrobble应如下所示:

 { "album[0]": "Achtung Baby",
   "album[1]": "Achtung Baby",
   "api_key":"xxxBLAHxxx",
   "api_sig":"8dbc147e533411a41ba9169f59e65b3a",
   "artist[0]": "U2",
   "artist[1]": "U2",          
   "method": "track.scrobble", 
   "sk":"kkkFOOkkk" 
   "timestamp[0]": "1379368800",
   "timestamp[1]": "1379369000",
   "track[0]": "One",
   "track[1]": "The Fly"
  } 

在转换为MD5哈希之前,唯一的另一个要点是确保dataToHash是UTF-8编码的。

修改

这就是我为ajax调用创建数据的方式。注意:这是未经测试的 - 我没有last.fm帐户。

var songsToScrobble = {};

function addDataToScrobble(parentElement, inputData) {
    if ($.isArray(inputData)) {
      $.each(inputData, function(index ,element) {
         songsToScrobble[parentElement + "[" + index + "]"] = element;
         dataToHash += parentElement + "[" + index + "]" + element;
      });
    } else {
      songsToScrobble[parentElement] = inputData;
      dataToHash += parentElement + inputData;
    }
}

for (var i = 0; i < data.tracks.length; i++) {
     albums.push(data.album);
     artists.push(data.artist);
     // The tracks are coming in the format: title|timestamp
     timestamps.push(data.tracks[i].split("|")[1]);
     tracks.push(data.tracks[i].split("|")[0]);     
}

addDataToScrobble("album", albums);
addDataToScrobble("api_key", apiKey);
addDataToScrobble("artist", artists);
addDataToScrobble("method", apiMethod);
addDataToScrobble("sk", key);
addDataToScrobble("timestamp", timestamps);
addDataToScrobble("track", tracks);

apiSig = $.md5(unescape(encodeURIComponent(dataToHash+apiSecret)));

songsToScrobble["api_sig"] = apiSig;

$.ajax({
    url: apiUrl,
    type: "POST",
    data: songsToScrobble,
    success: function (data) { 
        console.log(data);
    }
});