I'm trying to make members join a server using oauth2.
Whenever I use the api/guilds/${guild}/members/${user} endpoint, I get this error
{ message: '400: Bad Request', code: 0 }
The code I used:
let gid = '868120277077360660';
let usid = '591950387112574988';
fetch(`https://discord.com/api/guilds/${gid}/members/${usid}`, {
method: 'PUT',
access_token: `Bearer ${accessToken}`,
headers: {
authorization: `Bot ${botToken}`,
"Content-Type": "application/json",
},
payload: {
access_token: `Bearer ${accessToken}`,
}
})
.then(result => result.json())
.then(async reo => console.log(reo));
I'm using node-fetch as fetch, in the code. I'm using NodeJS.
I tried many solutions, but none of them worked, it would be great if you can guide me through!
Thank you!
Related
I'm looking to add a webhook generated via a discord bot directly to the creation of a repo on a team. Is this possible because I haven't seen an example in the GitHub doc.
Here is my code:
const request = await fetch(newUrl, {
headers: {
"Accept": "application/vnd.github+json",
'authorization': 'Bearer ' + getStringEnv("GITHUB_TOKEN"),
'content-type': 'application/json',
'X-GitHub-Api-Version': '2022-11-28'
},
method: "POST",
body: JSON.stringify({
name: name,
description: description,
private: true
})
})
console.log(await request.json())
}catch (e){
console.log(e)
}
I tried to read the documentation at this URL as well as all that can be said about webhooks:
https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads
I'm trying to fetch my guild info from discord API
fetch('https://discord.com/api/guilds/772037458996101140', {
headers: {
authorization: 'bot Nzc******zNjYzOTAz.GPwhCV.rhIcH****5R8ZS-cIo4MjPcBVxO6wYsUXhY'
},
})
.then(result => result.json())
.then(response => {
console.log(response);
})
.catch(console.error)
only reciving 401 (unauthorized)
i tried changing "bot" to "BOT", "Bot", "Bearer"
and doing authorization using access token from Oauth2
The problem lies in how the Authorization header has been specified. The correct way specify headers would be:
fetch('https://discord.com/api/guilds/772037458996101140', {
withCredentials: true,
credentials: 'include',
headers: {
'Authorization': 'bot Nzc******zNjYzOTAz.GPwhCV.rhIcH****5R8ZS-cIo4MjPcBVxO6wYsUXhY',
},
})
.then(result => result.json())
.then(response => {
console.log(response);
})
.catch(console.error)
Hope this helps.
Edit
Also added crendentials and withCredentials based on this answer.
I was sending http request from front-end and when i moved it to back-end or just used python test file it worked using : Authorization: 'Bot <TOKEN>'
not sure why but it works how it should mby somebody can explain ?
I want to send a post with fetching. But I get 401 error: www-authenticate: Bearer error="invalid_token".
I am using Userfront.accessToken() but It did not work.
How can I get accestoken for bearer authentication?
const submit = (e) => {
e.preventDefault();
const data = new FormData(form.current);
fetch(process.env.REACT_APP_ENDPOINT + "user/me/contract", {
method: "POST",
body: data,
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${Userfront.accessToken()}`,
},
}).then((res) => res.json());
};
Note:
console.log(`Bearer ${Userfront.accessToken()}`);
Bearer [object Object]
Can you try this? I see this from https://userfront.com/guide/auth/
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${Userfront.tokens.accessToken}`
}
JSON.stringify(Userfront.accessToken()) please stringify that object to understand what is going on there then if there is accessToken returning from that function put that string.
I just realized in the doc;
To handle a request like this -Userfront.accessToken()-, your backend should read the JWT from
the Authorization header and verify that it is valid using the public
key found in your Userfront dashboard.
https://userfront.com/guide/auth/
fetch('https://api.example.com', {
method: 'GET'
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${Userfront.tokens.accessToken}`
}
});
Thank you all for your answers.
Authorization: `Bearer ${localStorage.getItem("fray_access_token")}`,
In this application, token gets a different name.
When I look from inspects, I use it and it works!
I have a Post Request using fetch in react native, but I get a status code 400 for this, what is wrong with the code?
function sendRequest2() {
fetch(`https://sandbox-api.dexcom.com/v2/oauth2/token`, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: JSON.stringify({
code: "value1",
client_id: "value2",
client_secret: "value3",
redirect_uri: "http://www.google.com",
grant_type: "authorization_code",
}),
})
.then((response) => {
response.json();
})
.then((data) => {
console.log(data);
})
.catch((err) => console.log("The error is: " + err));
}
Check your content-type, replace it by :
headers: {
"Content-Type": "application/json",
},
helpfull link : application/x-www-form-urlencoded or multipart/form-data?
You can try by removing JSON.stringify
That would solve the issue
Moreover you have shared a lot of open information regarding your server openly.
You must either hide it or add some dummy values. Sharing such a secure data openly is not recommended in open community.
I'm making a bot that can change url code with a request, but i've tried something, and it's only returning the current invite code
My code:
fetch('https://www.discord.com/api/v6/guilds/762359120031907871/vanity-url', {
method: 'POST',
headers: { 'Authorization': 'Bot ' + client.token, 'Content-Type': 'application/json'},
payload: JSON.stringify({
"code":"test458"
})
})
.then(res => res.json())
.then(json => { console.log(json)})
And it's returning
{ code: 'cherrys', uses: 0 }
I just want to change the code, but idk how i can do that, and when i try with a user token it say
401: Unauthorized
Can someone help me with that?
The prefix Bot on the Autorization header is only for Bots, if you want to use a user token, you need to use Bearer prefix instead of the bot ones.
fetch('https://www.discord.com/api/v6/guilds/762359120031907871/vanity-url', {
method: 'POST',
headers: { 'Authorization': `Bearer ${client.token}`, 'Content-Type': 'application/json'},
payload: JSON.stringify({
"code":"test458"
})
})
.then(res => res.json())
.then(json => { console.log(json)});