javascript 为什么访问密钥api在汇率情况下不起作用?

z0qdvdin  于 2022-10-30  发布在  Java
关注(0)|答案(1)|浏览(194)

我尝试在exchange rate API中使用我的访问密钥,但收到错误消息。error: Object { code: "missing_access_key", message: "You have not supplied an API Access Key. [Required format: access_key=YOUR_ACCESS_KEY]" }代码片段i JS。

const API_url = "https://api.exchangeratesapi.io/v1/latest? access_key=[MyApiKey]";

async function currency(){
    const res = await fetch(API_url);
    const data = await res.json();
    console.log(data);
}
currency();
col17t5w

col17t5w1#

发布API文档中提到的代码片段。

var myHeaders = new Headers();
myHeaders.append("apikey", "{API-KEY}");

var requestOptions = {
  method: 'GET',
  redirect: 'follow',
  headers: myHeaders
};

fetch("https://api.apilayer.com/exchangerates_data/convert?to={to}&from={from}&amount={amount}", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

API密钥在头中发送,在代码片段中,它在GET请求的查询参数中发送。

相关问题