Marvel API 返回 409 冲突“您必须提供用户密钥”

发布于 2025-01-09 10:07:05 字数 1424 浏览 0 评论 0原文

我在这里查看了类似的问题,但似乎没有一个答案有帮助,因为他们没有使用普通 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);

https://developer.marvel.com/docs

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);

https://developer.marvel.com/docs

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぃ双果 2025-01-16 10:07:05

您的代码中有几个简单的问题:

  1. 您没有传递密钥(编辑已纠正此问题);

  2. 您没有对哈希字符串进行哈希处理。 文档显示hash需要是字符串的md5 时间戳+私钥+公钥。我搜索了如何在 JS 中对字符串进行 md5 处理,并遇到了 这个 SO 答案很多选择。我选择了第一个,并使用了链接的 md5 库 抱歉,我看到你当然是在进行哈希处理 - 你只是没有使用你生成的哈希值。您的 requestUrl 使用 hash 而不是您生成的 passhash

  3. 您没有传递 ts 参数。 文档中的示例表明您需要包含该内容;

  4. 您同时使用 nameStartsWithname,据我所知,您应该选择其中之一,而不是同时使用两者;

解决这些问题后,下面是调用 API 的基本 JS。密钥是假的,因此查询不起作用 - 但如果我们传递假密钥,错误是我们应该预料到的:传递的 API 密钥无效(单击“运行”即可查看)。如果您复制粘贴此代码,并在密钥中进行硬编码,它应该可以工作。

var marvelApiStart = "https://gateway.marvel.com:443/v1/public/characters?apikey=";
var marvelPublicKey = 'fake key';
var marvelPrivateKey = 'fake key';
var name = 'thor';
var ts = new Date().getTime();
var hash = md5(ts + marvelPrivateKey + marvelPublicKey);
var requestUrl = marvelApiStart + marvelPublicKey + "&ts=" + ts 
    + "&hash=" + hash + "&nameStartsWith=" + name;

console.log(requestUrl);

var result = fetch(requestUrl)
.then(function (response) {
    return response.json();
})
.then(function (data) {
    console.log(data);
});
<script src="https://www.myersdaily.org/joseph/javascript/md5.js"></script>

There are a couple of simple problems in your code:

  1. You weren't passing a key (an edit has since corrected this);

  2. You're not hashing your hash string. The docs show that hash needs to be an md5 of the string timestamp + 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. Sorry, I see you are of course hashing - you're just not using the hash you generate. Your requestUrl uses hash instead of the passhash you generated.

  3. You're not passing the ts parameter. The example in the docs shows you need to include that;

  4. You're using both nameStartsWith and name, 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.

var marvelApiStart = "https://gateway.marvel.com:443/v1/public/characters?apikey=";
var marvelPublicKey = 'fake key';
var marvelPrivateKey = 'fake key';
var name = 'thor';
var ts = new Date().getTime();
var hash = md5(ts + marvelPrivateKey + marvelPublicKey);
var requestUrl = marvelApiStart + marvelPublicKey + "&ts=" + ts 
    + "&hash=" + hash + "&nameStartsWith=" + name;

console.log(requestUrl);

var result = fetch(requestUrl)
.then(function (response) {
    return response.json();
})
.then(function (data) {
    console.log(data);
});
<script src="https://www.myersdaily.org/joseph/javascript/md5.js"></script>

鸠魁 2025-01-16 10:07:05

我这样做了:

  1. 在下一个脚本中添加到index.html:
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js"></script>

它是为了使用CryptoJs生成哈希

  1. 我有
API_KEY = 'my public api key'
BASE_URL = "https://gateway.marvel.com:443/v1/public/";
PRIV_KEY = 'my private api key'
  1. 创建函数从api获取数据:
export const getDataFromAPI = async (query) => {
  try {
    let ts = new Date().getTime();
    lethash = CryptoJS.MD5(ts + PRIV_KEY + API_KEY).toString();
    const url = `${BASE_URL}${query}?ts=${ts}&apikey=${API_KEY}&hash=${hash}`;
    const res = await fetch(url);
    const data = await res.json();
    console.log(data);
    return data;
  } catch (error) {
    console.log(error);
  }
}

查询可以是“字符”,“漫画”,“创作者”,“事件” ”、“系列”、“故事”。取决于端点

I did in this way:

  1. add to index.html in next script:
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js"></script>

It is for using CryptoJs which generate hash

  1. I had
API_KEY = 'my public api key'
BASE_URL = "https://gateway.marvel.com:443/v1/public/";
PRIV_KEY = 'my private api key'
  1. Create function for get data from api:
export const getDataFromAPI = async (query) => {
  try {
    let ts = new Date().getTime();
    lethash = CryptoJS.MD5(ts + PRIV_KEY + API_KEY).toString();
    const url = `${BASE_URL}${query}?ts=${ts}&apikey=${API_KEY}&hash=${hash}`;
    const res = await fetch(url);
    const data = await res.json();
    console.log(data);
    return data;
  } catch (error) {
    console.log(error);
  }
}

query can be 'characters', 'comics', 'creators', 'events', 'series', 'stories'. Depends on endpoint

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文