请告诉我我做错了什么,为什么我有403个axiosError?我使用的是React v17和axios v0.27.2和react聊天引擎v1.11.23。我怎样才能解决这个问题?
这里是我的imports
import React, { useState, useEffect, useContext } from "react";
import { useNavigate } from "react-router-dom";
import { ChatEngine } from "react-chat-engine";
import axios from "axios";
// Contexts
import { AuthContext } from "../contexts/AuthContextProvider";,这是主要代码
const Chats = () => {
const [loading, setLoading] = useState(true);
const user = useContext(AuthContext);
const navigate = useNavigate();
useEffect(() => {
if (!user) {
navigate("/");
return;
}
axios
.get("https://api.chatengine.io/users/me/", {
headers: {
"Project-ID": "6e1e7008-716b-4141-a15c-836f05f720dd",
"User-Name": user.email,
"User-Secret": user.uid,
},
})
.then(() => {
setLoading(false);
})
.catch(() => {
let formdata = new FormData();
formdata.append("email", user.email);
formdata.append("username", user.email);
formdata.append("secret", user.uid);
getFile(user.photoURL).then((avatar) => {
formdata.append("avatar", avatar, avatar.name);
axios
.post("https://api.chatengine.io/users/", formdata, {
headers: {
"private-key":
"8094d378-e224-4558-97bf-35ca877f8f8e",
},
})
.then(() => setLoading(false))
.catch((error) => console.log(error));
});
});
}, [user, navigate]);
const getFile = async (url) => {
const response = await fetch(url);
const data = await response.blob();
return new File([data], "userPhoto.jpg", { type: "image/jpeg" });
};
if (!user || loading) return "Loading...";
return (
<div>
<ChatEngine
height="calc(100vh - 50px)"
projectID="6e1e7008-716b-4141-a15c-836f05f720dd"
userName={user.email}
userSecret={user.uid}
/>
</div>
);
};
export default Chats;另外,当我点击api时,打开一个页面并编写HTTP 403禁忌。一般情况下,我无法访问api和聊天引擎。
谢谢你的帮助。
发布于 2022-07-03 00:57:32
要连接到api服务器,您需要一个放在项目仪表板API Keys部分的私钥。然后使用它作为axios头。您可以从正式的POST示例这里中看到该示例
headers: {
'PRIVATE-KEY': '{{private_key}}'
},发布于 2022-10-19 13:27:13
同样的代码,我也有同样的问题。我通过更改VPN服务来修复它。请检查您的私钥并与其他VPN服务进行测试。
https://stackoverflow.com/questions/72839077
复制相似问题