gpt4 book ai didi

javascript - 在 Jquery 中使用 Last.fm 进行身份验证 - 提供的方法签名无效

转载 作者:数据小太阳 更新时间:2023-10-29 03:55:01 27 4
gpt4 key购买 nike

我正在尝试授权 Last.fm session ,但正在努力正确签署 session key 请求。

我一直收到提供的无效方法签名 但是,当我对我认为查询应该包含 JS 之外的内容进行 md5 哈希时,我得到了相同的签名。我猜我一定是在字符串中包含了错误的数据,但不知道是什么。

我知道还有其他一些问题,我已经仔细检查了所有问题以了解这里出了什么问题,但我发誓它看起来对我来说是正确的。

这是签名算法和 Ajax 调用。我也试着留下足够的样本数据。

// Set elsewhere but hacked into this example:
var last_fm_data = {
'last_token':'TOKEN876234876',
'user': 'bob',
'secret': 'SECRET348264386'
};

// Kick it off.
last_fm_call('auth.getSession', {'token': last_fm_data['last_token']});


// Low level API call, purely builds a POSTable object and calls it.
function last_fm_call(method, data){
// param data - dictionary.

last_fm_data[method] = false;
// Somewhere to put the result after callback.

// Append some static variables
data['api_key'] = "APIKEY1323454";
data['format'] = 'json';
data['method'] = method;
post_data = last_fm_sign(data);

$.ajax({
type: "post",
url: last_url,
data: post_data,
success: function(res){
last_fm_data[method] = res;
console.log(res['key'])// Should return session key.
},
dataType: 'json'
});
}

function last_fm_sign(params){
ss = "";
st = [];
so = {};
Object.keys(params).forEach(function(key){
st.push(key); // Get list of object keys
});
st.sort(); // Alphabetise it
st.forEach(function(std){
ss = ss + std + params[std]; // build string
so[std] = params[std]; // return object in exact same order JIC
});
// console.log(ss + last_fm_data['secret']);
// api_keyAPIKEY1323454formatjsonmethodauth.getSessiontokenTOKEN876234876SECRET348264386
hashed_sec = unescape(encodeURIComponent($.md5(ss + last_fm_data['secret'])));
so['signature'] = hashed_sec; // Correct when calculated elsewhere.
return so; // Returns signed POSTable object
}

任何人都可以看到我在这里失踪了吗?我非常困惑为什么这没有以请求的格式返回正确签名的 POSTable 对象 here .谢谢你的时间。

编辑:如果我没有得到任何建议,就无法感谢任何人的宝贵时间!没有人使用过 last.fm 吗?

最佳答案

在调查了您的代码和其他与 last.fm api 调用相关的帖子后,我发现 @george lee 事实上是正确的。生成 auth_sign 时无需提供 format

除此之外,在应用 encodeURIComponent()unescape( ) 函数。像这样。

hashed_sec = $.md5(unescape(encodeURIComponent(ss + last_fm_data['secret'])));

此外,在进行 ajax 调用时,您需要将 api_key、token 和 api_sig 作为 data 传递。但是看到您的代码,表明您正在传递 api_key、token、format、method & signature

因此您需要从 ajax 调用的 data 字段中删除 format, method & signature

相反,您需要将 api_key、token 和 api_sig 传递给 data 字段。

所以注释 data['format'] = 'json'; 行后的最终代码将如下所示。

    // Set elsewhere but hacked into this example:
var last_fm_data = {
'last_token':'TOKEN876234876',
'user': 'bob',
'secret': 'SECRET348264386'
};

// Kick it off.
last_fm_call('auth.getSession', {'token': last_fm_data['last_token']});


// Low level API call, purely builds a POSTable object and calls it.
function last_fm_call(method, data){
// param data - dictionary.
last_fm_data[method] = false;
// Somewhere to put the result after callback.

// Append some static variables
data['api_key'] = "APIKEY1323454";
//data['format'] = 'json';
data['method'] = method;

post_data = last_fm_sign(data);

$.ajax({
type: "POST",
url: last_url,
data: post_data,
success: function(res){
last_fm_data[method] = res;
console.log(res['key'])// Should return session key.
},
dataType: 'json'
});
}

function last_fm_sign(params){
ss = "";
st = [];
so = {};
so['api_key'] = params['api_key'];
so['token'] = params['token'];
Object.keys(params).forEach(function(key){
st.push(key); // Get list of object keys
});
st.sort(); // Alphabetise it
st.forEach(function(std){
ss = ss + std + params[std]; // build string
});
ss += last_fm_data['secret'];
// console.log(ss + last_fm_data['secret']);
// api_keyAPIKEY1323454formatjsonmethodauth.getSessiontokenTOKEN876234876SECRET348264386
hashed_sec = $.md5(unescape(encodeURIComponent(ss)));
so['api_sig'] = hashed_sec; // Correct when calculated elsewhere.
return so; // Returns signed POSTable object
}

Please refer to this link.

关于javascript - 在 Jquery 中使用 Last.fm 进行身份验证 - 提供的方法签名无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30429806/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com