gpt4 book ai didi

javascript - 如何发出创建新用户的帖子请求

转载 作者:行者123 更新时间:2023-12-05 06:00:59 25 4
gpt4 key购买 nike

我正在使用 react-chat-engine 创建一个聊天应用程序。一切正常。除了我不知道如何发出创建新用户的帖子请求。

这是我的代码

const handelSubmit = async(e) => {
e.preventDefault();
if (!(username.length > 0 && password === confirmPassword && password.length > 0)) return;

const authObject = {
'Private-Key': '2f389292-d5e1-4799-bd31-b456e7e94845',
// 'Project-ID': '25a91c10-8623-4a0d-a48a-3de096d44b54',
// 'User-Name': username,
// 'User-Secret': password
}

const authHeader = {
'Private-Key': '2f389292-d5e1-4799-bd31-b456e7e94845',
'Project-ID': '25a91c10-8623-4a0d-a48a-3de096d44b54',
}
// const authBody = {
// 'username': username,
// 'secret': password
// }

try {
//fetch the current user if it exists
const userExists = await axios.get('https://api.chatengine.io/users', {
headers: authObject
});
console.log('worked');
console.log(userExists);
// if user already exists and in localStorage redirect them to the chat room
if (userExists && (localStorage.getItem('username') && localStorage.getItem('username') === username)) {
setTimeout(() => {
history.push('/chat')
}, 1000)
return;
}
console.log('worked2');
// if user don't exists, create a new user
await axios.post('https://api.chatengine.io/users', {
headers: authHeader,
body: {
'username': username,
'secret': password
}
});
// //saving user's username and password to localStorage
// localStorage.setItem('username', username);
// localStorage.setItem('password', password);
//redirecting them to the login page to login
console.log('worked25');
history.push('/login')
} catch (error) {
console.log(error);
setError(`oops, something went wrong`)
}

}

我正在尝试向用户添加新用户。但它一直向控制台抛出此错误。

Error: Request failed with status code 403
at createError (createError.js:16)
at settle (settle.js:17)
at XMLHttpRequest.handleLoad (xhr.js:62)

我能做什么?

最佳答案

尝试制作

const authHeader = {'Private-Key': '2f389292-d5e1-4799-bd31-b456e7e94845'}

仅在 https://api.chatengine.io/users/ 的末尾添加一个 / 我认为它会起作用

如果不是,那么您的私钥可能不正确。

(另外你不应该像这样在网上发布你的私钥,它是私有(private)的是有原因的。)

关于javascript - 如何发出创建新用户的帖子请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67394361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com