如何解决为什么这个网络钩子不起作用 - 一切似乎都很好?
代码在我的主机专用服务器上的 node.js 上运行。但是一直被拒绝,因为 disABLED_VERIFICATION_Failed。
我在服务器上打开了端口 8000,当我使用 Postman 发布时它可以工作 所以所有访问似乎都可以
拔我的头发 - 感谢任何帮助。
let smarclient; // Smartsheet JS client object
// Dependent libraries
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
app.use(bodyParser.json());
const smarSdk = require("smartsheet");
// Initialize client SDK
function initializeSmartsheetClient(token,logLevel) {
smarclient = smarSdk.createClient({
// If token is falsy,value will be read from SMARTSHEET_ACCESS_TOKEN environment variable
accesstoken: token,logLevel: logLevel
});
}
使用来自 Github 的代码,看起来不错
// Check that we can access the sheet
async function probeSheet(targetSheetId) {
console.log(`Checking for sheet id: ${targetSheetId}`);
const getSheetoptions = {
id: targetSheetId,queryParameters: { pageSize: 1 } // Only return first row to reduce payload
};
const sheetResponse = await smarclient.sheets.getSheet(getSheetoptions);
console.log(`Found sheet: "${sheetResponse.name}" at ${sheetResponse.permalink}`);
}
/*
* A webhook only needs to be created once.
* But hooks will be disabled if validation or callbacks fail.
* This method looks for an existing matching hook to reuse,else creates a new one.
*/
async function initializeHook(targetSheetId,hookName,callbackUrl) {
try {
let webhook = null;
// Get *all* my hooks
const listHooksResponse = await smarclient.webhooks.listWebhooks({
includeAll: true
});
console.log(`Found ${listHooksResponse.totalCount} hooks owned by user`);
// Check for existing hooks on this sheet for this app
for (const hook of listHooksResponse.data) {
if (hook.scopeObjectId === targetSheetId
&& hook.name === hookName
// && hook.callbackUrl === callbackUrl // Might be appropriate for your scenario
) {
webhook = hook;
console.log(`Found matching hook with id: ${webhook.id}`);
break;
}
}
if (!webhook) {
// Can't use any existing hook - create a new one
const options = {
body: {
name: hookName,callbackUrl,scope: "sheet",scopeObjectId: targetSheetId,events: ["*.*"],version: 1
}
};
const createResponse = await smarclient.webhooks.createWebhook(options);
webhook = createResponse.result;
console.log(`Created new hook: ${webhook.id}`);
}
// Make sure webhook is enabled and pointing to our current url
const options = {
webhookId: webhook.id,callbackUrl: callbackUrl,body: { enabled: true }
};
const updateResponse = await smarclient.webhooks.updateWebhook(options);
const updatedWebhook = updateResponse.result;
console.log(`Hook enabled: ${updatedWebhook.enabled},status: ${updatedWebhook.status}`);
} catch (err) {
console.error(err);
}
}
我使用 postman 测试了回调,端口 8000 在服务器上运行正常,所以不知道为什么它一直被拒绝?
// This method receives the webhook callbacks from Smartsheet
app.post("/",async (req,res) => {
const result = req.body;
//res.status(200).send("Im here");
try {
const body = req.body;
// Callback Could be due to validation,status change,or actual sheet change events
if (body.challenge) {
console.log("Received verification callback");
// Verify we are listening by echoing challenge value
res.status(200)
.json({ smartsheetHookResponse: body.challenge });
} else if (body.events) {
console.log(`Received event callback with ${body.events.length} events at ${new Date().toLocaleString()}`);
// Note that the callback response must be received within a few seconds.
// If you are doing complex processing,you will need to queue up pending work.
await processEvents(body);
res.sendStatus(200);
} else if (body.newWebHookStatus) {
console.log(`Received status callback,new status: ${body.newWebHookStatus}`);
res.sendStatus(200);
} else {
console.log(`Received unkNown callback: ${body}`);
res.sendStatus(200);
}
} catch (error) {
console.log(error);
res.status(500).send(`Error: ${error}`);
}
});
/*
* Process callback events
* This sample implementation only logs to the console.
* Your implementation might make updates or send data to another system.
* Beware of infinite loops if you make modifications to the same sheet
*/
async function processEvents(callbackData) {
if (callbackData.scope !== "sheet") {
return;
}
// This sample handles each event individually.
// Some changes (e.g. column rename) Could impact a large number of cells.
// A complete implementation should consolidate related events and/or cache intermediate data
for (const event of callbackData.events) {
// This sample only considers cell changes
if (event.objectType === "cell") {
console.log(`Cell changed,row id: ${event.rowId},column id ${event.columnId}`);
// Since event data is "thin",we need to read from the sheet to get updated values.
const options = {
id: callbackData.scopeObjectId,// Get sheet id from callback
queryParameters: {
rowIds: event.rowId.toString(),// Just read one row
columnIds: event.columnId.toString() // Just read one column
}
};
const response = await smarclient.sheets.getSheet(options);
const row = response.rows[0];
const cell = row.cells[0];
const column = response.columns.find(c => c.id === cell.columnId);
console.log(`**** New cell value "${cell.displayValue}" in column "${column.title}",row number ${row.rowNumber}`);
}
}
}
// main
(async () => {
try {
// Todo: Edit config.json to set desired sheet id and API token
const config = require("./config.json");
initializeSmartsheetClient(config.smartsheetAccesstoken,config.logLevel);
// Sanity check: make sure we can access the sheet
await probeSheet(config.sheetId);
app.listen(8000,() => console.log("Node-webhook-sample app listening on port 8000"));
await initializeHook(config.sheetId,config.webhookName,config.callbackUrl);
} catch (err) {
console.error(err);
}
})();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。