我开始学习JSON,在我的整个旅程中,我在使用API时还没有遇到过API密钥。我正在尝试使用pexels API,可以在这里找到https://www.pexels.com/api/documentation/?language=javascript。我得到了一个API密钥,我如何在JSON中使用它。我知道这是一个荒谬的问题,但我还不能找出解决方法。谢谢你的帮助。
发布于 2020-10-10 04:28:54
如果你通读文档,他们会告诉你如何使用你的API_KEY。
您可以查看docs here。
您必须在请求header
中将API_KEY
作为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))
发布于 2021-03-05 06:04:26
首先,我建议您在项目的根目录中创建一个名为.env.local的环境文件
然后将密钥添加到此env文件中
YOUR_API_KEY = '1232123123123123123' <== your key insted of these random numbers
然后,您可以安全地使用密钥进行fetch调用,然后像这样获取一些pexels endopint
export const getDataFromApi = async () => {
const res = await fetch(
ENDPOINT_URL,
{
headers: {
Authorization: API_KEY,
},
}
);
const responseJson = await res.json();
return responseJson.photos;
};
https://stackoverflow.com/questions/64286698
复制相似问题