我想在基于Firebase网络的项目中获取共享的Google电子表格。我为此使用了Firebase函数。首先,我通过使用google.auth.JWT获得令牌,然后尝试读取电子表格。部分地,我遵循How to use Google sheets API while inside a google cloud function。功能代码如下
exports.getNotes =functions.https.onRequest( (req, res) => {
var jwt = getJwt();
var apiKey = getApiKey();
var spreadsheetId = '<spreadsheet Id/>';
var range = 'Notes!A1:E1';
appendSheetRow(jwt, apiKey, spreadsheetId, range)
.then((result) => {
//console.log(result);
res.status(200).send(result);
})
.catch((err) => {
//console.log('error');
res.status(500).send({ err });
});
});
function getJwt() {
var credentials = require("./credentials.json");
var client = new google.auth.JWT(
credentials.client_email, null, credentials.private_key,
['https://www.googleapis.com/auth/spreadsheets','https://www.googleapis.com/auth/spreadsheets.readonly']
);
return new Promise((resolve, reject) => {
client.authorize((err, tokens) => {
if (err) {
reject(err);
} else {
console.log(tokens);
// google.options({
// auth: client
// });
resolve(tokens);
}
});
});
}
function getApiKey() {
var apiKeyFile = require("./api_key.json");
return apiKeyFile.key;
}
function appendSheetRow(jwt, apiKey, spreadsheetId, range, row) {
const sheets = google.sheets({version: 'v4'});
return new Promise((resolve, reject)=>{
console.log(spreadsheetId, range, jwt, apiKey);
sheets.spreadsheets.values.get({
spreadsheetId: spreadsheetId,
range: range,
auth: jwt,
key: apiKey,
},function(err, result){
if(err){
console.log(err);
return err;
}else{
console.log(result);
resolve(result.data.values);
//return result;
}
});
});
}
代码的Firebase日志结果为
2018-12-28T05:47:43.993155161Z D getNotes: Function execution started
2018-12-28T05:47:44.001Z I getNotes: hello
2018-12-28T05:47:44.087Z I getNotes: <spread sheetID/> Notes!A1:E1 Promise {
<pending> } <Key/>
2018-12-28T05:47:44.093Z I getNotes: TypeError: authClient.request is not a function
at /user_code/node_modules/googleapis/node_modules/googleapis-common/build/src/apirequest.js:225:31
at next (native)
at /user_code/node_modules/googleapis/node_modules/googleapis-common/build/src/apirequest.js:19:71
at __awaiter (/user_code/node_modules/googleapis/node_modules/googleapis-common/build/src/apirequest.js:15:12)
at createAPIRequestAsync (/user_code/node_modules/googleapis/node_modules/googleapis-common/build/src/apirequest.js:57:12)
at Object.createAPIRequest (/user_code/node_modules/googleapis/node_modules/googleapis-common/build/src/apirequest.js:49:9)
at Resource$Spreadsheets$Values.get (/user_code/node_modules/googleapis/build/src/apis/sheets/v4.js:566:37)
at Promise (/user_code/index.js:265:32)
at appendSheetRow (/user_code/index.js:263:10)
at exports.getNotes.functions.https.onRequest (/user_code/index.js:222:3)
2018-12-28T05:47:44.124Z I getNotes: { access_token: '<my token/>',
token_type: 'Bearer',
expiry_date: 1545979664000,
id_token: undefined,
refresh_token: 'jwt-placeholder' }
答案 0 :(得分:0)
这是我犯下的一个非常愚蠢的错误,问题是由于以下错误的解决方法
resolve(tokens)
但正确的解决方法如下所示
resolve(client)
正确的代码段
return new Promise((resolve, reject) => {
client.authorize((err, tokens) => {
if (err) {
reject(err);
} else {
// Notice here
resolve(client);
}
});
});
答案 1 :(得分:0)
TypeError: authClient.request is not a function
也有类似的问题,并将解决方法更改为客户端可以解决。
googleClient.authorize(async (err, tokens) => {
if (err) {
return res.badRequest({ success: false, error: err });
} else {
// Search for the sheet
// with auth (auth must be the auth client, not `tokens`)
let gsapi = google.sheets({
version: 'v4',
auth: googleClient
});
gsapi.spreadsheets.values.get(criteria, (err, res) => { ... })
}
}