Marvel API 返回 409 冲突“您必须提供用户密钥”
我在这里查看了类似的问题,但似乎没有一个答案有帮助,因为他们没有使用普通 JS。希望这里有人可以提供帮助。注意:如果有影响的话,也可以使用 IMDB API。 对于 API 来说真的很陌生,希望这只是我的一些愚蠢的错误。
var searchButton = document.getElementById("searchBtn");
var navContainer = document.getElementById("navContainer");
var userInput = document.getElementById("charSearch");
var savedSearches = JSON.parse(localStorage.getItem("hero")) || [];
var currentSearch = savedSearches.length;
var imdbApiStart = "https://imdb-api.com/en/API/Search/"
var imdbKey = "k_zcmn64r8/";
var marvelApiStart =
"https://gateway.marvel.com:443/v1/public/characters?apikey=";
var marvelKey = "public_key";
var marvelOtherKey = "my_private_key";
var ts = new Date().getTime();
var hash = ts +marvelKey+marvelOtherKey;
var passhash = CryptoJS.MD5(hash).toString();
console.log(passhash);
function getHeroInfo() {
getMovieInfo(userInput.value);
var requestUrl = marvelApiStart + marvelKey + "&hash=" + hash + "&name=" + userInput.value
console.log(requestUrl);
var result = fetch(requestUrl)
.then(function (response) {
return response.json();
})
.then(function (data) {
console.log(data);
});
return result;
}
function postHeroInfo(data) {
}
searchButton.addEventListener("click", getHeroInfo);
I have looked through similar questions on here and none of the responses seemed to help as they aren't using vanilla JS. Hopefully someone here can help out. Note: also using IMDB API if that makes a difference.
Really new to APIs and hoping this is just some stupid error on my part.
var searchButton = document.getElementById("searchBtn");
var navContainer = document.getElementById("navContainer");
var userInput = document.getElementById("charSearch");
var savedSearches = JSON.parse(localStorage.getItem("hero")) || [];
var currentSearch = savedSearches.length;
var imdbApiStart = "https://imdb-api.com/en/API/Search/"
var imdbKey = "k_zcmn64r8/";
var marvelApiStart =
"https://gateway.marvel.com:443/v1/public/characters?apikey=";
var marvelKey = "public_key";
var marvelOtherKey = "my_private_key";
var ts = new Date().getTime();
var hash = ts +marvelKey+marvelOtherKey;
var passhash = CryptoJS.MD5(hash).toString();
console.log(passhash);
function getHeroInfo() {
getMovieInfo(userInput.value);
var requestUrl = marvelApiStart + marvelKey + "&hash=" + hash + "&name=" + userInput.value
console.log(requestUrl);
var result = fetch(requestUrl)
.then(function (response) {
return response.json();
})
.then(function (data) {
console.log(data);
});
return result;
}
function postHeroInfo(data) {
}
searchButton.addEventListener("click", getHeroInfo);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码中有几个简单的问题:
您没有传递密钥(编辑已纠正此问题);
您没有对哈希字符串进行哈希处理。 文档显示抱歉,我看到你当然是在进行哈希处理 - 你只是没有使用你生成的哈希值。您的hash
需要是字符串的md5时间戳+私钥+公钥
。我搜索了如何在 JS 中对字符串进行 md5 处理,并遇到了 这个 SO 答案很多选择。我选择了第一个,并使用了链接的 md5 库。requestUrl
使用hash
而不是您生成的passhash
。您没有传递
ts
参数。 文档中的示例表明您需要包含该内容;您同时使用
nameStartsWith
和name
,据我所知,您应该选择其中之一,而不是同时使用两者;解决这些问题后,下面是调用 API 的基本 JS。密钥是假的,因此查询不起作用 - 但如果我们传递假密钥,错误是我们应该预料到的:
传递的 API 密钥无效
(单击“运行”即可查看)。如果您复制粘贴此代码,并在密钥中进行硬编码,它应该可以工作。There are a couple of simple problems in your code:
You weren't passing a key (an edit has since corrected this);
You're not hashing your hash string. The docs show thatSorry, I see you are of course hashing - you're just not using the hash you generate. Yourhash
needs to be an md5 of the stringtimestamp + privateKey + publicKey
. I searched for how to md5 a string in JS, and came across this SO answer with many options. I chose the first one, and used the linked md5 library.requestUrl
useshash
instead of thepasshash
you generated.You're not passing the
ts
parameter. The example in the docs shows you need to include that;You're using both
nameStartsWith
andname
, AFAICT you should either one or the other, not both;Fixing those things up, here is the basic JS to make a call to the API. The keys are fake, so the query does not work - but the error is what we should expect if we pass fake keys:
The passed API key is invalid
(click run to see it). If you copy-paste this code, and hardcode in your keys, it should work.我这样做了:
它是为了使用CryptoJs生成哈希
查询可以是“字符”,“漫画”,“创作者”,“事件” ”、“系列”、“故事”。取决于端点
I did in this way:
It is for using CryptoJs which generate hash
query can be 'characters', 'comics', 'creators', 'events', 'series', 'stories'. Depends on endpoint