You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails集成Google Calendar推送通知缺失X-Goog请求头问题排查

问题:Rails集成Google Calendar V3推送通知缺失关键请求头

已通过OAuth2.0完成授权,可正常对Google日历执行增删改事件操作,但配置推送通知后,webhook端点触发时无法收到预期的X-Goog-Channel-ID、X-Goog-Channel-Token、X-Goog-Resource-ID、X-Goog-Resource-URI请求头,即使创建新事件也无此类头信息。

一、推送通知配置代码

def sync_google_events
    client = Signet::OAuth2::Client.new(client_options(callback_url))
    client.update!(session[:authorization])
    service = set_calender_service(client)

    channel = Google::Apis::CalendarV3::Channel.new(address: 'ngrok_url', id: SecureRandom.uuid, kind: 'api#channel', type: 'web_hook', token: SecureRandom.uuid)
    response = service.watch_event('primary', channel, single_events: true)
end

二、watch_event接口响应

@expiration = 1702980528000,
@id="e5xxxxxxxxxxxxxxxxx",
@kind="api#channel",
@resource_id="xxxxxxxxxxxxxxxxxxx",
@resource_uri="https://www.googleapis.com/calendar/v3/calendars/primary/events?alt=json&singleEvents=true",
@token="xxxxxxxxxxxxxxxxxx"

三、Webhook端点代码

class WebhookController < ApplicationController
  skip_before_action :verify_authenticity_token, only: :webhook

  def webhook
    puts 'Some changes have been made in google calender'
  end
end

四、当前收到的Webhook响应

@cache_control={},
@committed=false,
@cv=#<MonitorMixin::ConditionVariable:0x00007fed0cef0b68 @cond=#<Thread::ConditionVariable:0x00007fed0cef0a78>, @monitor=#<Monitor:0x00007fed0cef0e10>>,
@header=
 {
  "X-Frame-Options"=>"SAMEORIGIN",
  "X-XSS-Protection"=>"0",
  "X-Content-Type-Options"=>"nosniff",
  "X-Download-Options"=>"noopen",
  "X-Permitted-Cross-Domain-Policies"=>"none",
  "Referrer-Policy"=>"strict-origin-when-cross-origin"
 },
@mon_data=#<Monitor:0x00007fed0cef0e10>,
@mon_data_owner_object_id=370,
@request=#<ActionDispatch::Request POST "894e.ngrok-free.app/webhook" for 74.125.210.10>,
@sending=false,
@sent=false,
@status=200,
@stream=#<ActionDispatch::Response::Buffer:0x00007fed0cef0c58 @buf=[], @closed=false, @response=#<ActionDispatch::Response:0x00007fed0cef0eb0 ...>, @str_body=nil>>

排查建议

  • 修正请求头获取方式:你当前打印的是Rails响应对象的header,而非Google发送的请求头。修改webhook方法,直接打印请求头:
    def webhook
      # 筛选并打印Google相关请求头
      puts "Google推送请求头: #{request.headers.select { |k, _| k.start_with?('X-Goog-') }}"
      # 返回200状态码给Google,避免重复推送
      head :ok
    end
    
  • 确认webhook地址为HTTPS:Google要求推送通知的address必须是HTTPS协议,确保你的ngrok地址以https://开头。
  • 检查ngrok请求详情:通过ngrok的web管理界面(通常是http://localhost:4040)查看Google发送的完整请求,确认是否包含目标请求头,排除请求被拦截的可能。
  • 验证订阅有效性:确认watch_event返回的expiration时间未过期,若订阅已过期需重新创建;同时核对resource_uri是否指向正确的日历资源。
  • 排查Rails中间件干扰:部分安全中间件可能会过滤或修改请求头,可暂时禁用非必要中间件,测试是否能正常接收头信息。
  • 确认权限范围:确保OAuth2.0授权的权限范围包含https://www.googleapis.com/auth/calendar(或更精细的日历操作权限),权限不足可能导致推送信息不完整。

内容的提问来源于stack exchange,提问作者Vranda Agrawal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 08:57:50