我正在尝试从公共Google表中访问数据。该表可以读取任何人的访问。我正在使用官方node.js客户端。我正在使用一个服务帐户来验证请求,因为我正在使用同一服务帐户访问另一个无法公开的表。
该代码正常工作,但是一旦我将Node.js客户端更新为最新版本,它就开始给我带来奇怪的错误。我创建了一个错误的示例,以下是 -
的代码/*eslint-disable no-console */
const { promisify } = require('util');
const GoogleAuth = require('google-auth-library');
const { google } = require('googleapis');
const googleAuth = new GoogleAuth();
const sheets = google.sheets('v4');
sheets.spreadsheets.values.getAsync = promisify(sheets.spreadsheets.values.get);
async function authorizeWithServiceAccount(serviceAccountKey, scopes) {
try {
let authClient = await authorize(serviceAccountKey, scopes);
return authClient;
} catch (err) {
console.error(err);
throw err;
}
}
function authorize(credentials, scopes) {
return new Promise((resolve, reject) => {
googleAuth.fromJSON(credentials, (err, client) => {
if (err) {
console.error(err);
reject(err);
return;
}
client.scopes = scopes;
client.authorize((err, result) => {
if (err) {
console.error(err);
reject(err);
return;
}
console.log(result, true);
resolve(client);
});
});
});
}
async function getData(auth, spreadsheetId, range) {
try {
return sheets.spreadsheets.values.getAsync({
auth: auth,
spreadsheetId: spreadsheetId,
range: range
});
} catch (e) {
console.error(e);
throw e;
}
}
const serviceAccountJson = require('../configs/keys/service_account'); //The service account json key
const spreadsheetId = 'SPREADSHEET_ID'; // Id of the sheet I am trying to access
const apiKey = 'THE_API_KEY'; //Any API key generated on Google's API console
const range = 'A:M';
async function init() {
let authClient = await authorizeWithServiceAccount(serviceAccountJson, [
'https://www.googleapis.com/auth/spreadsheets.readonly'
]);
return getData(authClient, spreadsheetId, range); //This doesn't work and throw error
// return getData(apiKey, spreadsheetId, range); //This does work and return all the data.
}
init()
.then(result => {
console.log('Received Data');
console.log(result.data);
})
.catch(e => console.error(e));
因此,如果我使用API密钥而不是服务帐户作为auth参数,我会根据预期获得正确的数据。但是,一旦我使用服务帐户, result.data
就变成 undefined
,然后得到此错误。
TypeError: callback is not a function
at JWT.OAuth2Client.postRequest (/Volumes/Projects/Work/node_modules/google-auth-library/lib/auth/oauth2client.js:341:9)
at postRequestCb (/Volumes/Projects/Work/node_modules/google-auth-library/lib/auth/oauth2client.js:297:23)
at Request._callback (/Volumes/Projects/Work/node_modules/google-auth-library/lib/transporters.js:113:17)
at Request.self.callback (/Volumes/Projects/Work/node_modules/request/request.js:186:22)
at emitTwo (events.js:126:13)
at Request.emit (events.js:214:7)
at Request.<anonymous> (/Volumes/Projects/Work/node_modules/request/request.js:1163:10)
at emitOne (events.js:116:13)
at Request.emit (events.js:211:7)
at IncomingMessage.<anonymous> (/Volumes/Projects/Work/node_modules/request/request.js:1085:12)
我正在使用Googleapis库25.x版本,当时服务帐户auth正在工作,但是一旦我将其更新为28.x,它就停止了工作。
有什么办法可以在28.x googleapis node.js客户端中使用服务帐户而不是API密钥?我无法降级它,因为我使用的是其他需要最新版本的Google API。
好吧,我再次查看了文档,他们在一个地方提到了有关如何做的文档。我以前在使用类似的Google -auth库 -
const GoogleAuth = require('google-auth-library');
const googleAuth = new GoogleAuth();
在上一个文档中提到的。我猜在API的文档中,不记得了。但是他们现在正在使用Googleapis软件包以及床单API支持Auth。因此,我要做的就是切换到使用Auth。因此,这就是我现在正在获得Authclient的方式,并且在测试中正常工作。
const { google } = require('googleapis');
const authClient = await google.auth.getClient({
credentials: credentials,
scopes: scopes
});
现在,我使用最新的googleapis软件包/node.js客户端获得了正确的数据。
所以问题是我如何获得身份验证者。较旧的方式似乎与最新客户端不兼容。
理论:sheets.values.get做事很奇怪, promisify
无法正常工作。
可能的修复:手动promisify
getData
async function getData(auth, spreadsheetId, range) {
return new Promise((resolve, reject) => {
sheets.spreadsheets.values.get({
auth: auth,
spreadsheetId: spreadsheetId,
range: range
}, (error, result) => {
if (error) {
return reject(error);
}
resolve(result);
});
});
}