8
n8n 中文网amn8n.com

使用Google Drive推送通知监控文件变更

高级

这是一个File Management领域的自动化工作流,包含 29 个节点。主要使用 If, Set, Code, Filter, Webhook 等节点。 通过Google Drive推送通知监控文件变更

前置要求
  • HTTP Webhook 端点(n8n 会自动生成)
  • 可能需要目标 API 的认证凭证
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "meta": {
    "instanceId": "408f9fb9940c3cb18ffdef0e0150fe342d6e655c3a9fac21f0f644e8bedabcd9"
  },
  "nodes": [
    {
      "id": "d8c73782-4560-4cce-bd59-767defcf3317",
      "name": "设置变量",
      "type": "n8n-nodes-base.set",
      "position": [
        -2576,
        320
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "5cba7460-3ff9-44ac-80d8-a6d649d19dd5",
              "name": "driveId",
              "type": "string",
              "value": "REPLACE_ME_DRIVE_ID"
            },
            {
              "id": "667d142b-d36c-4ee4-a672-dcc247649960",
              "name": "channelId",
              "type": "string",
              "value": "REPLACE_ME_CUSTOM_CHANNEL_NAME"
            },
            {
              "id": "8f1cc4e7-2dbc-47a8-bdd4-222598531684",
              "name": "channelToken",
              "type": "string",
              "value": "REPLACE_ME_CUSTOM_CHANNEL_TOKEN"
            },
            {
              "id": "c4446141-516d-4f44-b39e-eaff25356db6",
              "name": "webhookUrl",
              "type": "string",
              "value": "={{ 'https://' + $execution.resumeUrl.extractDomain() + '/webhook/' +$('Webhook').params.path }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "e3f4a25d-f75a-49f3-b935-8822e9465f87",
      "name": "获取 StartPageToken",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -1856,
        144
      ],
      "parameters": {
        "url": "https://www.googleapis.com/drive/v3/changes/startPageToken",
        "options": {},
        "sendQuery": true,
        "authentication": "predefinedCredentialType",
        "queryParameters": {
          "parameters": [
            {
              "name": "supportsAllDrives",
              "value": "true"
            },
            {
              "name": "driveId",
              "value": "={{ $('Set Variables').first().json.driveId }}"
            }
          ]
        },
        "nodeCredentialType": "googleDriveOAuth2Api"
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "yOwz41gMQclOadgu",
          "name": "Google Drive account"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "0fe67ac8-2166-42bc-be0d-9cb99b9091b2",
      "name": "注册 Webhook",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -1680,
        144
      ],
      "parameters": {
        "url": "https://www.googleapis.com/drive/v3/changes/watch",
        "method": "POST",
        "options": {},
        "jsonBody": "={\n  \"id\": \"{{ $('Set Variables').first().json.channelId }}\",\n  \"type\": \"web_hook\",\n  \"address\": \"{{ $('Set Variables').first().json.webhookUrl }}\",\n  \"token\": \"{{ $('Set Variables').first().json.channelToken }}\",\n  \"expiration\": {{ $now.plus({ \"days\": 7 }).toMillis() }}\n}",
        "sendBody": true,
        "sendQuery": true,
        "specifyBody": "json",
        "authentication": "predefinedCredentialType",
        "queryParameters": {
          "parameters": [
            {
              "name": "pageToken",
              "value": "={{ $json.startPageToken }}"
            },
            {
              "name": "supportsAllDrives",
              "value": "true"
            },
            {
              "name": "includeItemsFromAllDrives",
              "value": "true"
            },
            {
              "name": "driveId",
              "value": "={{ $('Set Variables').first().json.driveId }}"
            }
          ]
        },
        "nodeCredentialType": "googleDriveOAuth2Api"
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "yOwz41gMQclOadgu",
          "name": "Google Drive account"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "caefc780-c8e4-47c9-a8ce-4656e0f73264",
      "name": "将注册保存到 WorkflowStaticData",
      "type": "n8n-nodes-base.code",
      "position": [
        -1504,
        144
      ],
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "// Get the global workflow static data\nconst workflowStaticData = $getWorkflowStaticData('global');\n\nconst registration = $input.item.json;\n\n// Update its data\nconst lastPageToken = $('Get StartPageToken').first().json.startPageToken;\n\nworkflowStaticData.registration = registration;\nworkflowStaticData.lastPageToken = lastPageToken;\n\nreturn { workflowStaticData }"
      },
      "typeVersion": 2
    },
    {
      "id": "655de901-1a33-4bd3-b2b7-441f5ec9f4ec",
      "name": "是有效请求吗?",
      "type": "n8n-nodes-base.if",
      "position": [
        -816,
        304
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "a490cbb7-febd-4266-8fb1-4e557ee325d2",
              "operator": {
                "name": "filter.operator.equals",
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.headers['x-goog-channel-id'] }}",
              "rightValue": "={{ $json.workflowStaticData.registration.id }}"
            },
            {
              "id": "0ce9621d-f200-4093-8b22-bfbb422790c0",
              "operator": {
                "name": "filter.operator.equals",
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.headers['x-goog-channel-token'] }}",
              "rightValue": "={{ $json.workflowStaticData.registration.token }}"
            },
            {
              "id": "4403843f-5e74-4e11-929b-1085726f12bf",
              "operator": {
                "name": "filter.operator.equals",
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.headers['x-goog-resource-state'] }}",
              "rightValue": "change"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "bfc39f92-6196-47f2-947c-05e728bb4853",
      "name": "无效请求",
      "type": "n8n-nodes-base.noOp",
      "position": [
        -592,
        400
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "03d5afa2-42eb-4a08-a387-8291bed5245a",
      "name": "获取更改列表",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -592,
        224
      ],
      "parameters": {
        "url": "https://www.googleapis.com/drive/v3/changes",
        "options": {},
        "sendQuery": true,
        "authentication": "predefinedCredentialType",
        "queryParameters": {
          "parameters": [
            {
              "name": "pageToken",
              "value": "={{ $json.workflowStaticData.lastPageToken || 10000 }}"
            },
            {
              "name": "supportsAllDrives",
              "value": "true"
            },
            {
              "name": "includeItemsFromAllDrives",
              "value": "true"
            },
            {
              "name": "driveId",
              "value": "={{ $json.driveId }}"
            }
          ]
        },
        "nodeCredentialType": "googleDriveOAuth2Api"
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "yOwz41gMQclOadgu",
          "name": "Google Drive account"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "6484466f-868b-480b-b022-753a4cd19738",
      "name": "将更改事件转换为项目",
      "type": "n8n-nodes-base.splitOut",
      "position": [
        -192,
        656
      ],
      "parameters": {
        "options": {},
        "fieldToSplitOut": "changes"
      },
      "typeVersion": 1
    },
    {
      "id": "36530836-1816-4483-a2b5-6bb98ea31e2c",
      "name": "过滤更改事件",
      "type": "n8n-nodes-base.filter",
      "position": [
        0,
        656
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "b76abb68-b859-47d9-b952-cedd0055a4f9",
              "operator": {
                "type": "boolean",
                "operation": "false",
                "singleValue": true
              },
              "leftValue": "={{ $json.removed }}",
              "rightValue": ""
            },
            {
              "id": "24878191-ad0d-4572-95b5-878f1eed5f28",
              "operator": {
                "name": "filter.operator.equals",
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.type }}",
              "rightValue": "file"
            },
            {
              "id": "1b1fcda9-25e7-4b1b-ae9a-1899bbacc359",
              "operator": {
                "name": "filter.operator.equals",
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.changeType }}",
              "rightValue": "file"
            },
            {
              "id": "daf1be34-d87f-479c-adc2-e155ab3d4474",
              "operator": {
                "name": "filter.operator.equals",
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.file.mimeType }}",
              "rightValue": "application/pdf"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "94b0b1e2-2bc5-4de2-a32c-4f28b71806e9",
      "name": "从 WorkflowStaticData 获取注册信息",
      "type": "n8n-nodes-base.code",
      "position": [
        -1008,
        304
      ],
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "const staticData = $getWorkflowStaticData('global');\nreturn {\n  ...$input.item.json,\n  workflowStaticData: staticData,\n  driveId: staticData.registration.resourceUri.match(/driveId=([^&]+)&/)[1]\n}"
      },
      "typeVersion": 2
    },
    {
      "id": "ed3fc6b5-a952-4805-be6e-359b6d54e7a9",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2880,
        0
      ],
      "parameters": {
        "color": 7,
        "width": 1552,
        "height": 640,
        "content": "## 1. 注册 Google Drive 通知通道 (Webhook)"
      },
      "typeVersion": 1
    },
    {
      "id": "e573fe58-f2fb-4c92-bd88-2dfc473e5392",
      "name": "计划触发器",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        -2800,
        320
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "daysInterval": 6,
              "triggerAtHour": 6
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "2d48f008-c590-4e53-9569-611be082d5c6",
      "name": "便签 2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1280,
        0
      ],
      "parameters": {
        "color": 7,
        "width": 896,
        "height": 640,
        "content": "## 2. 监听推送通知"
      },
      "typeVersion": 1
    },
    {
      "id": "2a02d923-bd2d-48b3-975d-a03949eb50dc",
      "name": "获取文件详情",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        192,
        656
      ],
      "parameters": {
        "url": "=https://www.googleapis.com/drive/v3/files/{{ $json.file.id }}",
        "options": {},
        "sendQuery": true,
        "authentication": "predefinedCredentialType",
        "queryParameters": {
          "parameters": [
            {
              "name": "fields",
              "value": "id,kind,name,createdTime,modifiedTime,mimeType,trashed,driveId,parents"
            },
            {
              "name": "supportsAllDrives",
              "value": "true"
            }
          ]
        },
        "nodeCredentialType": "googleDriveOAuth2Api"
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "yOwz41gMQclOadgu",
          "name": "Google Drive account"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "193af0b7-26b7-4ef4-b727-f222f58dc323",
      "name": "便签 3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -320,
        0
      ],
      "parameters": {
        "color": 7,
        "width": 720,
        "height": 416,
        "content": "## 3. 为下一次推送通知更新 LastPageToken"
      },
      "typeVersion": 1
    },
    {
      "id": "44bae3f1-ac15-4313-a229-733921c80660",
      "name": "便签 4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -320,
        448
      ],
      "parameters": {
        "color": 7,
        "width": 720,
        "height": 416,
        "content": "## 4. 后过滤更改列表"
      },
      "typeVersion": 1
    },
    {
      "id": "cbaee96d-e408-4c02-b05c-14a8d5348d56",
      "name": "便签 1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        64,
        208
      ],
      "parameters": {
        "width": 384,
        "height": 144,
        "content": "### ⚠️ 仅在生产模式下工作!"
      },
      "typeVersion": 1
    },
    {
      "id": "2cbebd1f-a927-40b1-989f-8e960669de51",
      "name": "便签 5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -3552,
        -416
      ],
      "parameters": {
        "width": 608,
        "height": 1344,
        "content": "## 使用 Google 推送通知监视 Google Drive 文件更改!"
      },
      "typeVersion": 1
    },
    {
      "id": "719e5e52-7424-409e-b9cf-dc746f8af3f7",
      "name": "对这些文件执行操作!",
      "type": "n8n-nodes-base.noOp",
      "position": [
        496,
        656
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "902b64b1-0acf-4922-8ffe-a4114e6a2fdd",
      "name": "便签6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2016,
        352
      ],
      "parameters": {
        "color": 7,
        "width": 608,
        "content": ""
      },
      "typeVersion": 1
    },
    {
      "id": "5e7ca713-097e-4e50-9ca3-6c5faaea1a02",
      "name": "将 NextPageToken 保存到 WorkflowStaticData",
      "type": "n8n-nodes-base.code",
      "position": [
        -160,
        224
      ],
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "// Get the global workflow static data\nconst workflowStaticData = $getWorkflowStaticData('global');\n\n// Update its data\nconst lastPageToken = $json.newStartPageToken || $json.nextPageToken;\n\nworkflowStaticData.lastPageToken = lastPageToken;\n\nreturn { workflowStaticData }"
      },
      "typeVersion": 2
    },
    {
      "id": "e2972a38-5c2f-40b5-965b-4ab7bc10df6a",
      "name": "获取注册信息",
      "type": "n8n-nodes-base.code",
      "position": [
        -2400,
        320
      ],
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "return $getWorkflowStaticData('global') ?? {};"
      },
      "typeVersion": 2
    },
    {
      "id": "12893d4d-0a38-4f1c-b5d5-fc5d76c4889c",
      "name": "便签7",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2624,
        272
      ],
      "parameters": {
        "width": 192,
        "height": 304,
        "content": ""
      },
      "typeVersion": 1
    },
    {
      "id": "24fd55eb-d91f-413d-97e8-636e4e19c494",
      "name": "首次运行?点击这里!",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        -2800,
        704
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "947ad96f-d564-4943-9deb-cc93a92c63fb",
      "name": "便签8",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -3552,
        960
      ],
      "parameters": {
        "width": 608,
        "height": 336,
        "content": "![](https://cdn.subworkflow.ai/n8n-templates/banner_595x311.png#full-width)"
      },
      "typeVersion": 1
    },
    {
      "id": "bdfbaa00-65f0-4392-86d0-2213301737a9",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "position": [
        -1216,
        304
      ],
      "webhookId": "44fac054-fc68-4360-9069-377bd3c6bc40",
      "parameters": {
        "path": "44fac054-fc68-4360-9069-377bd3c6bc40",
        "options": {},
        "httpMethod": "POST"
      },
      "typeVersion": 2
    },
    {
      "id": "e06f30f0-9eaf-4098-bbd7-7ed6fbf3a2b9",
      "name": "Subworkflow 触发器",
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "position": [
        -2800,
        464
      ],
      "parameters": {
        "inputSource": "passthrough"
      },
      "typeVersion": 1.1
    },
    {
      "id": "2e74455d-d95a-4575-afe4-e57c2da3e9d3",
      "name": "停止任何现有通知",
      "type": "n8n-nodes-base.httpRequest",
      "onError": "continueRegularOutput",
      "position": [
        -2224,
        320
      ],
      "parameters": {
        "url": "https://www.googleapis.com/drive/v3/channels/stop",
        "method": "POST",
        "options": {},
        "sendBody": true,
        "authentication": "predefinedCredentialType",
        "bodyParameters": {
          "parameters": [
            {
              "name": "id",
              "value": "={{ $json.registration?.id ?? $('Set Variables').first().json.channelId }}"
            },
            {
              "name": "resourceId",
              "value": "={{ $json.registration?.resourceId }}"
            },
            {
              "name": "token",
              "value": "={{ $json.registration?.token ?? $('Set Variables').first().json.channelToken }}"
            }
          ]
        },
        "nodeCredentialType": "googleDriveOAuth2Api"
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "yOwz41gMQclOadgu",
          "name": "Google Drive account"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "466e4f97-c801-43f5-a90f-3a493e47d912",
      "name": "注册通知通道",
      "type": "n8n-nodes-base.executeWorkflow",
      "position": [
        -2592,
        704
      ],
      "parameters": {
        "mode": "each",
        "options": {},
        "workflowId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $workflow.id }}"
        },
        "workflowInputs": {
          "value": {},
          "schema": [],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": true
        }
      },
      "typeVersion": 1.2
    }
  ],
  "pinData": {},
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Get Registration From WorkflowStaticData",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Variables": {
      "main": [
        [
          {
            "node": "Get Registration",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Changes List": {
      "main": [
        [
          {
            "node": "Save NextPageToken to WorkflowStaticData",
            "type": "main",
            "index": 0
          },
          {
            "node": "Change Events to Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Registration": {
      "main": [
        [
          {
            "node": "Stop Any Existing Notifications",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Register Webhook": {
      "main": [
        [
          {
            "node": "Save Registration to WorkflowStaticData",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Set Variables",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Files Details": {
      "main": [
        [
          {
            "node": "Do Something With These Files!",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Valid Request?": {
      "main": [
        [
          {
            "node": "Get Changes List",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Invalid Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get StartPageToken": {
      "main": [
        [
          {
            "node": "Register Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Subworkflow Trigger": {
      "main": [
        [
          {
            "node": "Set Variables",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter Changed Events": {
      "main": [
        [
          {
            "node": "Get Files Details",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Change Events to Items": {
      "main": [
        [
          {
            "node": "Filter Changed Events",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "First Run? Click Here!": {
      "main": [
        [
          {
            "node": "Register Notification Channel",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Stop Any Existing Notifications": {
      "main": [
        [
          {
            "node": "Get StartPageToken",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Registration From WorkflowStaticData": {
      "main": [
        [
          {
            "node": "Is Valid Request?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

高级 - 文件管理

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
高级
节点数量29
分类1
节点类型13
难度说明

适合高级用户,包含 16+ 个节点的复杂工作流

作者
Jimleuk

Jimleuk

@jimleuk

Freelance AI Automation Engineer based in London, UK. Since 2024, my n8n templates have documented my journey into applied AI and have helped hundreds of businesses and organisations get up to speed with AI automation. Today, I continue to explore use-cases as AI evolves and occasionally upload templates which I find novel and interesting. Subscribe to the RSS Feed: https://cdn.subworkflow.ai/n8n-templates/rss.xml

外部链接
在 n8n.io 查看

分享此工作流