Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon web services 使用AWS Lambda/API网关授权google服务帐户_Amazon Web Services_Aws Api Gateway_Serverless_Service Accounts - Fatal编程技术网

Amazon web services 使用AWS Lambda/API网关授权google服务帐户

Amazon web services 使用AWS Lambda/API网关授权google服务帐户,amazon-web-services,aws-api-gateway,serverless,service-accounts,Amazon Web Services,Aws Api Gateway,Serverless,Service Accounts,我的express服务器有一个credentials.json,其中包含google服务帐户的凭据。这些凭证用于从google获得jwt,我的服务器使用jwt更新服务帐户拥有的google表单 var jwt_client=null; //从本地文件加载凭据 fs.readFile('./private/credentials.json',(err,content)=>{ if(err)返回console.log('加载客户端机密文件时出错:',err); //使用凭据授权客户端,然后调用Goo

我的express服务器有一个credentials.json,其中包含google服务帐户的凭据。这些凭证用于从google获得jwt,我的服务器使用jwt更新服务帐户拥有的google表单

var jwt_client=null;
//从本地文件加载凭据
fs.readFile('./private/credentials.json',(err,content)=>{
if(err)返回console.log('加载客户端机密文件时出错:',err);
//使用凭据授权客户端,然后调用Google Sheets API。
授权(JSON.parse(content));
});
//获取JWT
功能授权(凭证){
const{client_email,private_key}=凭证;
jwt_client=new google.auth.jwt(client_email,null,private_key,SCOPES);
}
var sheets=google.sheets({version:'v4',auth:jwt_client});
//此时,我可以调用GoogleAPI并发出授权请求
问题是我正试图从node/express迁移到npm-serverless/aws。我使用相同的代码,但得到403-禁止

 errors:
   [ { message: 'The request is missing a valid API key.',
       domain: 'global',
       reason: 'forbidden' } ] }

研究指出了很多事情,包括:。所有这些对我来说似乎都是可行的,但我是AWS的新手,因此如果您能给我一些建议,我将不胜感激。

现在已经晚了,但可能会对其他人有所帮助。这是我的工作代码

const {google} = require('googleapis');
const KEY = require('./keys');
const _ = require('lodash');

const sheets = google.sheets('v4');

const jwtClient = new google.auth.JWT(
    KEY.client_email,
    null,
    KEY.private_key,
    [
        'https://www.googleapis.com/auth/drive',
        'https://www.googleapis.com/auth/drive.file',
        'https://www.googleapis.com/auth/spreadsheets'
    ],
    null
);

async function getGoogleSheetData() {
    await jwtClient.authorize();
    const request = {
            // The ID of the spreadsheet to retrieve data from.
            spreadsheetId: 'put your id here',  
    
            // The A1 notation of the values to retrieve.
            range: 'put your range here',  // TODO: Update placeholder value.
            auth: jwtClient,
        };
   return await sheets.spreadsheets.values.get(request)
}

然后在lambda处理程序中调用它。我不喜欢将key.json作为文件存储在项目根目录中。我会设法找个更好的地方存钱。

时间不早了,但可能会帮助其他人。这是我的工作代码

const {google} = require('googleapis');
const KEY = require('./keys');
const _ = require('lodash');

const sheets = google.sheets('v4');

const jwtClient = new google.auth.JWT(
    KEY.client_email,
    null,
    KEY.private_key,
    [
        'https://www.googleapis.com/auth/drive',
        'https://www.googleapis.com/auth/drive.file',
        'https://www.googleapis.com/auth/spreadsheets'
    ],
    null
);

async function getGoogleSheetData() {
    await jwtClient.authorize();
    const request = {
            // The ID of the spreadsheet to retrieve data from.
            spreadsheetId: 'put your id here',  
    
            // The A1 notation of the values to retrieve.
            range: 'put your range here',  // TODO: Update placeholder value.
            auth: jwtClient,
        };
   return await sheets.spreadsheets.values.get(request)
}
然后在lambda处理程序中调用它。我不喜欢将key.json作为文件存储在项目根目录中。我会设法找到更好的地方来保存