首页 > 解决方案 > JSON:使用 Pexels API 密钥

问题描述

我开始学习 JSON,在我的整个旅程中,我在使用 API 时没有遇到过 API 密钥。我正在尝试使用 pexels API,在这里找到https://www.pexels.com/api/documentation/?language=javascript。我获得了一个 API 密钥,如何在 JSON 中使用它。我知道这是一个荒谬的问题,但我无法找出方法。谢谢你的帮助。

标签: javascriptjsonapikey

解决方案


如果你仔细阅读他们给出的如何使用你的 API_KEY 的文档。

你可以在这里查看文档。

您必须将API_KEY请求header作为Authorization.

curl -H "Authorization: YOUR_API_KEY" \
  "https://api.pexels.com/v1/search?query=people"

JS

fetch("https://api.pexels.com/v1/search?query=people", {
  headers: {
    Authorization: 'YOUR_API_KEY'
  })
  .then(response => response.json())
  .then(result => console.log(result))
  .catch(err => console.log(err))

推荐阅读