在Google Cloud Functions上使用Google Sheets API,getClient()无法正常工作,函数超时

问题描述 投票:0回答:1

我一直在尝试获取Google Cloud Functions以便通过getClient()自动获取默认的服务帐户凭据,但是当我使用它时,该函数没有响应,最终会超时。我现在仅使用Google Sheets API来获取值。

我已确保授予服务帐户正确的访问权限,与服务电子邮件共享工作表,检查我的依存关系。

我已经看到一些使用getClient的答案和指南,但是我似乎无法使它正常工作,这是我的代码:

const {google} = require('googleapis');
const express = require('express');
const bodyParser = require('body-parser');

const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

const PORT = process.env.PORT || 8080;
const SHEET_ID = "1ie8......lvnaKM";
const SCOPES = ['https://www.googleapis.com/auth/spreadsheets'];

app.get('/', (req, res) => {

    let range = 'test2!B1';
    getSheetsValue(SHEET_ID, range, res);

});

app.listen(PORT, () => { });

exports.sheetsTest = app;

async function getSheetsValue(sheetID, range, res) {

    const authClient = await google.auth.getClient({
        scopes: SCOPES,
    });
    console.log(authClient);
    const sheets = google.sheets({
        version: 'v4',
        authClient
    });
    request = {
        spreadsheetId: sheetID,
        range: range,
    };
    try {
        const result = await sheets.spreadsheets.values.get(request);
        console.log(`result: ${JSON.stringify(result)}`);
        console.log(`result.data: ${JSON.stringify(result.data)}`);
        res.json(result.data.values[0]);
    } catch (e) {
        return e;
    }
}

我甚至已经按照这里的说明使用google-auth-library尝试过,但是没有用:https://github.com/googleapis/google-auth-library-nodejs#application-default-credentials

直到将凭据硬编码到程序中之前,我只是希望弄清楚为什么它不起作用。

谢谢

node.js google-cloud-functions google-sheets-api google-authentication google-api-nodejs-client
1个回答
0
投票

首先,应确保在PATH中创建了一个名为GOOGLE_APPLICATION_CREDENTIALS的变量,该变量应指向凭据JSON,如果需要更多信息,可以检查here的操作方法。如果您不这样做,则getClient()将不起作用。

关于您的代码,您似乎在这里做了一些奇怪的事情:

 const authClient = await google.auth.getClient({
        scopes: SCOPES,
    });

如果您查看要使用的身份验证方法的documentation,您将看到代码看起来不是这样。将其更改为类似的内容。

 const auth = new GoogleAuth({
        scopes: SCOPES
 });

 const authClient = await auth.getClient();

但是现在您需要将此类添加到脚本中,因此在“ imports”上添加以下行:

const {GoogleAuth} = require('google-auth-library');

您最后一次出错的地方是在这段代码中:

 const sheets = google.sheets({
        version: 'v4',
        authClient
    });

您似乎并没有真正告知变量property指的是什么authClient。所以你应该将其更改为

 const sheets = google.sheets({
        version: 'v4',
        auth: authClient
    });

所以对我来说,最终代码看起来像这样:

const {google} = require('googleapis');
const {GoogleAuth} = require('google-auth-library');
const express = require('express');
const bodyParser = require('body-parser');

const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

const PORT = process.env.PORT || 8080;
const SHEET_ID = "<your sheet ID>";
const SCOPES = ['https://www.googleapis.com/auth/spreadsheets'];

app.get('/', (req, res) => {

    let range = 'test2!B1';
    getSheetsValue(SHEET_ID, range, res);

});

app.listen(PORT, () => { });

exports.sheetsTest = app;

async function getSheetsValue(sheetID, range, res) {

    const auth = new GoogleAuth({
        scopes: SCOPES
      });

    const authClient = await auth.getClient();
    // console.log(authClient);
    const sheets = google.sheets({
        version: 'v4',
        auth: authClient
    });
    request = {
        spreadsheetId: sheetID,
        range: range,
    };
    try {
        const result = await sheets.spreadsheets.values.get(request);
        console.log(`result: ${JSON.stringify(result)}`);
        console.log(`result.data: ${JSON.stringify(result.data)}`);
        res.json(result.data.values[0]);
    } catch (e) {
        console.log(e);
        return e;
    }
}

© www.soinside.com 2019 - 2024. All rights reserved.