I am trying to use Discord API Oauth2 to automatically add a user to my guild. Everything what i need is to get access token for this action as data to send in send(data), user discord id and token of my bot app. Everything of this i already have
So when i am starting my scirpt with my tilda.cc site or with my dedicated server using django i got this error everytime
Access to XMLHttpRequest at 'https://discord.com/api/v8/guilds/myguild/members/my_acc' from origin 'http://dedic_ip:8000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
or in tilda
Access to XMLHttpRequest at 'https://discord.com/api/v8/guilds/myguild/members/my_acc' from origin 'http://myproject.tilda.ws' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Here is my code
<script>
const makeRequest = (method, url, data) => {
const xhr = new XMLHttpRequest();
xhr.withCredentials = true;
return new Promise(resolve => {
xhr.open(method, url, true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('Authorization', 'Bot my_bot_token');
xhr.onload = () => resolve({
status: xhr.status,
response: xhr.response
});
xhr.onerror = () => resolve({
status: xhr.status,
response: xhr.response
});
if (method == 'GET') xhr.send();
if (method != 'GET') xhr.send(data);
})
}
const test = async() => {
console.log("Starting request ...");
var data = JSON.stringify({'access_token': 'my_token'})
console.log("data:", data);
let request4 = await makeRequest("PUT", 'https://discord.com/api/v8/guilds/myguild_id/members/my_acc_id', data)
console.log("status:", request4.status);
console.log("response:", request4.response);
}
test()
</script>
I am new in web so maybe it is useless info but in network console after preflight i have neeaded headers. But PUT request didn't have them option request put request
Also important to mention that i successfully did this PUT request using python requests, my acc was added to guild and no errors happened with this code:
url = f"{API_ENDPOINT}/guilds/{guild}/members/{user['id']}"
data = {
"access_token": access_token,
}
headers = {
"Authorization": f"Bot {botToken}",
'Content-Type': 'application/json'
}
response = requests.put(url=url, headers=headers, json=data)