Я пытаюсь получить доступ к данным из общедоступной таблицы 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 вместо учетной записи службы в качестве параметра аутентификации, я получаю правильные данные, как и ожидалось. Но как только вместо этого я использую учетную запись службы, 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, и тогда авторизация учетной записи службы работала, но как только я обновил его до 28.x, он перестал работать.
Есть ли способ использовать учетную запись службы вместо ключа API в узле 28.x googleapis .js клиент? Я не могу перейти на более раннюю версию, так как использую другие API Google, для которых требуется последняя версия.