Function/function.js
Unknown 46deb1c0ef a
2024-06-04 17:46:01 -07:00

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" });
}
};