Github, add discord webhook by repo organisation id - javascript

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

Related

React how to get accessToken?

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!

JavaScript (Discord Oauth2) 400: Bad Request

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!

Error 400 on spotify web api. Need hlp pls

Sorry for this title but i really need hlp. I don't know why that's not working and i searched a lot.
So I'm working with Spotify Api and I want to access to the Access_Token. The documentation says you have to do like that: Spotify Documentation
And I m requesting like this :
fetch ('https://accounts.spotify.com/api/token', {
method: 'post',
body: {
code: code,
redirect_uri: redirectUri,
grant_type: 'authorization_code'
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Authorization: 'Basic ' + btoa(clientId+':'+clientSecret)
},
json: true
})
But that's answering this:
Error
I checked and error 400 means "Bad Request".
Have u got an idea? Thanks for helping !
Looking at your error, no body was received. You have to send it as a json string:
let body = {
code: code,
redirect_uri: redirectUri,
grant_type: 'authorization_code'
}
fetch ('https://accounts.spotify.com/api/token', {
method: 'post',
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Authorization: 'Basic ' + btoa(clientId+':'+clientSecret)
},
json: true
})

How to add attachment to gmail api in js

I am trying to add couple of image attachments in google api, I am not able to find any kind of solution for it, though I am able to send mail without it, can any one help on adding attachments.
const encodedMail = btoa([
`From: ${guest_email}\r\n`,
`To: ${toHotel}\r\n`,
`Subject: ${fullname} Sent Mail\r\n\r\n`,
`Hello There\n\n I have Just Mailed, Please find my details\n\n Name: ${fullname}\n
Phone: ${phone}\n `
].join('')).replace(/\+/g, '-').replace(/\//g, '_');
const response = await fetch(url, {
method: 'POST',
mode: 'cors',
cache: 'no-cache',
credentials: 'same-origin',
headers: {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/json'
},
redirect: 'follow',
referrerPolicy: 'no-referrer',
body: JSON.stringify({
'raw': encodedMail
})
});
if (response) {
console.log(response)
}
Help would be really appreciated.

Mailchimp api to subscribe using java script request issue

I am trying below javascript code to subscribe on mailchimp. flow wise working good but i am getting error like not authorize because may be i am not passing api key correctly. please help to solve my issue
var request = new Request('https://<dc>.api.mailchimp.com/3.0/lists/[listid]/members', {
method: 'POST',
mode: 'no-cors',
json: {
"email_address": "am_it#live.com",
"status": "subscribed",
},
redirect: 'follow',
headers: new Headers({
'Content-Type': 'application/json',
'Authorization': 'Basic apikey'
})
});
// Now use it!
fetch(request).then(function (data) {
console.log(data);
});
You need to add your authentification details with your username and api key. You can do it with the auth parameter:
auth: {
'user': 'yourUserName',
'pass': 'yourApiKey'
}
Just add that to your request object:
Request('https://<dc>.api.mailchimp.com/3.0/lists/[listid]/members', {
method: 'POST',
mode: 'no-cors',
json: {
"email_address": "am_it#live.com",
"status": "subscribed",
},
redirect: 'follow',
headers: new Headers({
'Content-Type': 'application/json',
'Authorization': 'Basic apikey'
}),
auth: {
'user': 'yourUserName',
'pass': 'yourApiKey'
}
});
I looked at the documentation in the getting started section of the developer mailchimp api: here and I converted the curl examples to javascript code using this page. Let me know if it worked.

Categories

Resources