几个小时以来,我一直在为一个涉及Firebase Cloud Functions&Google Sheets API。我只是在向Cloud Function发出POST
请求时,尝试读取电子表格中的一些单元格。
下面的代码,我使用函数readCells
从电子表格中读取,效果很好:
const {google} = require('googleapis');
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
var db = admin.firestore();
// magic function (?)
function readCells(auth, ssheetId) {
const sheets = google.sheets({version: 'v4', auth});
return new Promise((resolve, reject) => {
sheets.spreadsheets.values.get({spreadsheetId:ssheetId,
range:'MySheet!A1:A2'},
(err, result) => {
if (err) {
reject(err);
}
resolve(result);
});
});
}
exports.test = functions.https.onRequest((request, response) => {
// read auth token & spreadsheet ID from Cloud Firestore
let tokenPromise = db.doc('config/token').get();
let spreadsheetPromise = db.doc('config/spreadsheet').get();
// read from spreadsheet
return Promise.all([tokenPromise, spreadsheetPromise]).then(results => {
const token = results[0].data();
const ssheetId = results[1].data().id;
const oAuth2Client = new google.auth.OAuth2(token.client_id, token.client_secret, "");
oAuth2Client.setCredentials(token);
return readCells(oAuth2Client, ssheetId);
})
.then(result => {
console.log(result.data.values);
return response.status(200).end();
})
.catch(err => {
console.error(err);
return response.status(500).end();
});
});
然后我去掉readCells
,在HTTPS
云函数test
:中做所有事情
exports.test = functions.https.onRequest((request, response) => {
// read auth token & spreadsheet ID from Cloud Firestore
let tokenPromise = db.doc('config/token').get();
let spreadsheetPromise = db.doc('config/spreadsheet').get();
// read from spreadsheet
return Promise.all([tokenPromise, spreadsheetPromise]).then(results => {
const token = results[0].data();
const ssheetId = results[1].data().id;
const oAuth2Client = new google.auth.OAuth2(token.client_id, token.client_secret, "");
oAuth2Client.setCredentials(token);
const sheets = google.sheets({version: 'v4', oAuth2Client});
return new Promise((resolve, reject) => {
sheets.spreadsheets.values.get({spreadsheetId:ssheetId,
range:'MySheet!A1:A2'},
(err, result) => {
if (err) {
reject(err);
}
resolve(result);
});
});
})
.then(result => {
console.log(result.data.values);
return response.status(200).end();
})
.catch(err => {
console.error(err);
return response.status(500).end();
});
});
但我得到以下错误:
error: { Error: The request is missing a valid API key.
at createError (/<path_to_functions>/node_modules/axios/lib/core/createError.js:16:15)
at settle (/<path_to_functions>/node_modules/axios/lib/core/settle.js:18:12)
at IncomingMessage.handleStreamEnd (/<path_to_functions>/node_modules/axios/lib/adapters/http.js:201:11)
at emitNone (events.js:111:20)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
...
more stuff
...
还有:
error: (node:3308) UnhandledPromiseRejectionWarning:
Unhandled promise rejection. This error originated either by throwing
inside of an async function without a catch block,
or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:3308) [DEP0018] DeprecationWarning: Unhandled promise rejections
are deprecated. In the future, promise rejections that are not handled
will terminate the Node.js process with a non-zero exit code.
我完全不理解这种行为。我只是在不使用函数的情况下做同样的事情。我对Javascript或Node.js不是很有经验;任何帮助都将不胜感激。
我设法通过更换线来解决问题
const sheets = google.sheets({version: 'v4', oAuth2Client});
带有
const sheets = google.sheets('v4');
并将认证客户端直接传递给CCD_ 6功能。
所以它变成了这样:
exports.test = functions.https.onRequest((request, response) => {
let tokenPromise = db.doc('config/token').get();
let spreadsheetPromise = db.doc('config/spreadsheet').get();
return Promise.all([tokenPromise, spreadsheetPromise]).then(results => {
const token = results[0].data();
const ssheetId = results[1].data().id;
const oAuth2Client = new google.auth.OAuth2(token.client_id, token.client_secret);
oAuth2Client.setCredentials(token);
const sheets = google.sheets('v4');
return new Promise((resolve, reject) => {
sheets.spreadsheets.values.get({spreadsheetId: ssheetId,
auth: oAuth2Client,
range: "MySheet!A1:A2"}, (err, result) => {
if (err) {
reject(err);
}
resolve(result);
});
})
})
.then(result => {
console.log(result.data.values[0][0]);
return response.status(200).end();
})
.catch(err => {
console.error(err);
return response.status(500).end();
});
});