API调用失败并显示401

时间:2020-07-15 17:12:20

标签: javascript api

我正在使用javascript和axios库向Halo Developers'API(www.haloapi.com)发出获取请求。我的代码如下:

async function get() {
    let getData = async () => {
        let url = "https://www.haloapi.com/stats/h5/companies/111-222-333-444?key=[my key]";
        var response = await axios.get(url);
        return response.data;
    }
    let reply = await getData();
    return reply
}

(为您的查看而简化),执行此代码将产生401 api键丢失异常。但是,与我的方法类似,除了url之外,其他方法都很好。内容如下:

async function get() {
    let getData = async () => {
        let url = "https://www.haloapi.com/profile/h5/profiles/myplayername/appearance?key=[my key]";
        var response = await axios.get(url);
        return response.data;
    }
    let reply = await getData();
    return reply
}

为什么会这样?它与破折号有关吗?我真的很茫然。

1 个答案:

答案 0 :(得分:0)

这是正确的代码:

axios.get(url, { 'headers': { 'Ocp-Apim-Subscription-Key': key } });