mirror of
https://github.com/System-End/Function.git
synced 2026-04-19 22:05:13 +00:00
33 lines
1 KiB
JavaScript
33 lines
1 KiB
JavaScript
exports.ExchangeTokenFunction = async (req, res) => {
|
|
try {
|
|
const { code } = req.body;
|
|
|
|
// Exchange authorization code for access token
|
|
const tokenEndpoint = "https://oauth2.googleapis.com/token";
|
|
const tokenParams = new URLSearchParams({
|
|
client_id:
|
|
"49121286862-vihhl46cr7v5acqjp99nuuc001dnfs4q.apps.googleusercontent.com",
|
|
client_secret: "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",
|
|
code: code,
|
|
redirect_uri: "http://localhost:5500/Pages/callback.html",
|
|
grant_type: "authorization_code",
|
|
});
|
|
|
|
// Use dynamic import() to import node-fetch
|
|
const fetch = await import("node-fetch");
|
|
|
|
const response = await fetch.default(tokenEndpoint, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
},
|
|
body: tokenParams,
|
|
});
|
|
|
|
const data = await response.json();
|
|
res.json(data); // Return the access token to the client
|
|
} catch (error) {
|
|
console.error("Error:", error);
|
|
res.status(500).json({ error: "Internal Server Error" });
|
|
}
|
|
};
|