无法使用 dialogflow 从 google 上的操作写入 Google 电子表格
Unable to write to Google Spreadsheet from actions on google with dialogflow
我正在尝试使用 Dialogflow 从 Google 应用上的 Actions 写入 Google Spreadsheet。我可以使用我的应用程序从 sheet 中读取。传播sheet 被授予 public 编辑权限。
我在 fulfillment webhook 中使用以下代码来读取和写入 spreadsheet。我有我的 SPREADSHEET_ID 和 SPREADSHEET_API_KEY:
function welcome(agent) {
const tabName = 'Sheet1';
const startCell = 'B2';
const endCell = 'D';
appendDataToSpreadsheet(tabName, startCell, endCell);
agent.add(`Appended`);
}
function appendDataToSpreadsheet(tabName, startCell, endCell) {
const sheets = google.sheets({version: 'v4', auth: SPREADSHEET_API_KEY});
return sheets.spreadsheets.values.append({
auth: auth,
spreadsheetId: SPREADSHEET_ID,
range: `${tabName}!${startCell}:${endCell}`,
valueInputOption: "USER_ENTERED",
resource: {
values: [ ["5", "Anis", "8", "React"], ["6", "Paul", "1", "Python"] ]
}
}, (err, response) => {
if (err) {
console.log('The API returned an error: ' + err);
return;
} else {
console.log("Appended");
}
});
}
此代码不会将数据附加到传播sheet,并且在我的日志中我可以看到以下错误:
The API returned an error:
Error: Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project
.
该应用正在请求身份验证凭据。关于如何解决这个问题的任何指示都会有所帮助。
为了写入 sheet,您需要获得 authorization 才能这样做,即使对于可公开写入的 sheet 也是如此,因为写入需要用户帐户才能注释了谁进行了更改。虽然 API 密钥足以读取 sheet(因为没有审核日志),但您需要一个帐户才能实际写入。
如果您只是通过操作执行此操作,则可以为单个用户维护授权令牌和刷新令牌并将其隐藏。然而,更好的方法是使用 Google Sign In for Assistant and an 来准确跟踪谁编辑了传播 sheet.
我建议通过设置使用 JWT-OAuth2.0 的服务帐户进行身份验证:
const spreadsheetId = 'INSERT_HERE';
const serviceAccount = { INSERT_HERE }; // {"type": "service_account",...
// Set up Google Calendar Service account credentials
const serviceAccountAuth = new google.auth.JWT({
email: serviceAccount.client_email,
key: serviceAccount.private_key,
scopes: 'https://www.googleapis.com/auth/spreadsheet'
});
您可以通过在 Dialogflow 项目设置 或 https://console.cloud.google.com/home/dashboard?project=INSERT_AGENT_NAME 中转到 Google 云平台 link 来完成此操作。
从那里转到 API 和服务 > 凭据 > 创建凭据 > 服务帐户密钥 > Project/Owner。
我正在尝试使用 Dialogflow 从 Google 应用上的 Actions 写入 Google Spreadsheet。我可以使用我的应用程序从 sheet 中读取。传播sheet 被授予 public 编辑权限。
我在 fulfillment webhook 中使用以下代码来读取和写入 spreadsheet。我有我的 SPREADSHEET_ID 和 SPREADSHEET_API_KEY:
function welcome(agent) {
const tabName = 'Sheet1';
const startCell = 'B2';
const endCell = 'D';
appendDataToSpreadsheet(tabName, startCell, endCell);
agent.add(`Appended`);
}
function appendDataToSpreadsheet(tabName, startCell, endCell) {
const sheets = google.sheets({version: 'v4', auth: SPREADSHEET_API_KEY});
return sheets.spreadsheets.values.append({
auth: auth,
spreadsheetId: SPREADSHEET_ID,
range: `${tabName}!${startCell}:${endCell}`,
valueInputOption: "USER_ENTERED",
resource: {
values: [ ["5", "Anis", "8", "React"], ["6", "Paul", "1", "Python"] ]
}
}, (err, response) => {
if (err) {
console.log('The API returned an error: ' + err);
return;
} else {
console.log("Appended");
}
});
}
此代码不会将数据附加到传播sheet,并且在我的日志中我可以看到以下错误:
The API returned an error:
Error: Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. Seehttps://developers.google.com/identity/sign-in/web/devconsole-project
.
该应用正在请求身份验证凭据。关于如何解决这个问题的任何指示都会有所帮助。
为了写入 sheet,您需要获得 authorization 才能这样做,即使对于可公开写入的 sheet 也是如此,因为写入需要用户帐户才能注释了谁进行了更改。虽然 API 密钥足以读取 sheet(因为没有审核日志),但您需要一个帐户才能实际写入。
如果您只是通过操作执行此操作,则可以为单个用户维护授权令牌和刷新令牌并将其隐藏。然而,更好的方法是使用 Google Sign In for Assistant and an
我建议通过设置使用 JWT-OAuth2.0 的服务帐户进行身份验证:
const spreadsheetId = 'INSERT_HERE';
const serviceAccount = { INSERT_HERE }; // {"type": "service_account",...
// Set up Google Calendar Service account credentials
const serviceAccountAuth = new google.auth.JWT({
email: serviceAccount.client_email,
key: serviceAccount.private_key,
scopes: 'https://www.googleapis.com/auth/spreadsheet'
});
您可以通过在 Dialogflow 项目设置 或 https://console.cloud.google.com/home/dashboard?project=INSERT_AGENT_NAME 中转到 Google 云平台 link 来完成此操作。
从那里转到 API 和服务 > 凭据 > 创建凭据 > 服务帐户密钥 > Project/Owner。