[英]JSON: Use Pexels API key
我開始學習 JSON,在我的整個旅程中,我在使用 API 時沒有遇到過 API 密鑰。 我正在嘗試使用 pexels API,在這里找到https://www.pexels.com/api/documentation/?language=javascript 。 我得到了一個 API 密鑰,我如何在 JSON 中使用它。 我知道這是一個荒謬的問題,但我一直無法找到方法。 謝謝你的幫助。
如果您閱讀文檔,他們會給出如何使用您的 API_KEY。
你可以在這里查看文檔。
您必須在請求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))
首先,我建議您在項目的根目錄中創建一個名為 .env.local 的 env 文件
然后將密鑰添加到此 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;
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.