3

我已经尝试了一段时间了,但我无法弄清楚出了什么问题。也许我已经尝试了很多方法,以至于我什至不确定这段代码是否正确。

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

这就是我拥有的代码,它总是返回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)。还有什么问题?

4

1 回答 1

0

对您的代码做了一个快速的示例 JS Fiddle

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);
    }
});
于 2013-09-16T22:30:25.047 回答