[我有一个点击操作,将获取请求发送到运行Google auth脚本的AWS Lambda函数,然后返回一个URL进行授权并返回到获取请求,并使用window.location
将我发送给Google以进行授权,授权,目前我已将其发送回同一个lambda。我不能只是将其发送回Gatsby站点,因为google需要auth重定向URL来返回200状态代码,而且我不能仅在我的站点上创建一个页面,例如/ auth进行重定向。因此,一旦Im重定向到原始lambda,授权代码就会附加到网址中。到目前为止,一切正常。
我坚持的是下一步。
A)如何将用户重定向回Gatsby网站?
B)我是否将该身份验证(从url参数中提取出来)存储在我可以使用faundDB的数据库中,我很熟悉那和lambda函数。
C)并且我应该将google发送到一个单独的lambda,然后将我的提取请求发送给它,这真的很重要。
我正在使用NodeJS Lambda
这是我的lambda
const { google } = require("googleapis");
// const axios = require('axios')
// const url = 'http://checkip.amazonaws.com/';
let response;
/**
*
* Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format
* @param {Object} event - API Gateway Lambda Proxy Input Format
*
* Context doc: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html
* @param {Object} context
*
* Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
* @returns {Object} object - API Gateway Lambda Proxy Output Format
*
*/
exports.lambdaHandler = async (event, context) => {
try {
const oauth2Client = new google.auth.OAuth2(
"1fdsfas",
"uiPCvLfsdfs",
"https://fdsfa/test"
);
// generate a url that asks permissions for Blogger and Google Calendar scopes
const scopes = ["https://www.googleapis.com/auth/calendar"];
const url = oauth2Client.generateAuthUrl({
// 'online' (default) or 'offline' (gets refresh_token)
access_type: "offline",
// If you only need one scope you can pass it as a string
scope: scopes,
});
console.log(url);
// const ret = await axios(url);
response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify({
message: "Hello World",
urlAuth: url,
// location: ret.data.trim()
}),
};
} catch (err) {
console.log(err);
return err;
}
return response;
};
这是我的提取请求
const click = () => {
fetch("http://fdsaf/test")
.then(response => response.json())
.then(response => {
console.log(response.urlAuth)
window.location.href = response.urlAuth
})
}
// const axios = require('axios')
// const url = 'http://checkip.amazonaws.com/';
let response;
const faunadb = require("faunadb"); /* Import faunaDB sdk */
exports.lambdaHandler = async (event, context) => {
try {
// const ret = await axios(url);
let code;
if (event.queryStringParameters) {
code = event.queryStringParameters.code;
} else {
code = "no code";
}
// Connect to the database
const q = faunadb.query;
const client = new faunadb.Client({
secret: "fdsafa",
});
await client
.query(q.Create(q.Collection("AuthUrl"), { data: { title: code } }))
.then((ret) => console.log(ret));
response = {
statusCode: 301,
headers: {
Location: "http://localhost:8000",
},
};
} catch (err) {
console.log(err);
return err;
}
return response;
};