我正在尝试使用 Axios 访问 Uber API,但遇到了一些问题。我已将此数据插入 Postman,并且毫无问题地获得了 200 响应代码。但是,当我尝试进行 Axios 调用时,我得到未经授权的响应代码 401。我能否获得一些帮助来查看我的代码以找出我的授权无法与 Axios 一起正常工作的原因?
这是我引用的 Uber API 文档的链接。 Uber API Reference
getRide_Uber = async (addressOrigin, addressDestination) => {
let origin = await geocodeAddress(addressOrigin);
let destination = await geocodeAddress(addressDestination);
const url = "https://api.uber.com/v1.2/estimates/price";
const params = {
params: {
start_latitude: origin.lat,
start_longitude: origin.lon,
end_latitude: destination.lat,
end_longitude: destination.lon
}
};
const headers = {
headers: {
Authorization: `Token ${process.env.UBER_SERVER_TOKEN}`
}
};
const response = await axios
.get(url, params, headers)
.then(function(response) {
data = response.data;
})
.catch(function(error) {
console.log(error);
});
return data;
};
如果有任何需要澄清的地方,请告诉我。谢谢!
最佳答案
试试下面的语法,
const config = {
headers: {
Authorization: `Token ${process.env.UBER_SERVER_TOKEN}`
}
params: {
start_latitude: origin.lat,
start_longitude: origin.lon,
end_latitude: destination.lat,
end_longitude: destination.lon
}
};
const response = await axios
.get(url, config)
.then(function(response) {
data = response.data;
})
.catch(function(error) {
console.log(error);
});
return data;
axios 还有一个方面,async/await 在 Internet Explorer 和旧版浏览器中不支持。所以也请检查您的浏览器版本。
关于rest - 尝试授权 Axios get 请求时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55155177/